又一次尝试将 Polymer 与 ES6 类结合起来。
除了 wct 测试在具有导入的 es6 类(由 systemjs)的聚合物组件上随机失败外,它几乎可以正常工作。据我了解,发生这种情况是因为包含类定义的脚本在 mocha 执行测试后被加载。聚合物组件由两部分组成 - html 和 javascript(将后者编译为 es5),
html:
<dom-module id="my-test">
<template>hello</template>
<script>
System.import('elements/my-test.js');
</script>
</dom-module>
JavaScript:
import {User} from 'elements/model/user.js';
Polymer({
is: "my-test",
method: function(){
console.log("method, user="+this.val);
},
ready: function(){
this.user= new User(); //this.user is randomly undefined
}
});
这似乎在浏览器中工作得相当稳定,至少在从本地主机加载时是这样。但唯一“修复”测试的是延迟 Polymer 的 ready 调用:
Polymer.whenReady = function (f) {
console.log("polymer ready");
setTimeout(f, 100);// "fix"
//f();
}
这意味着在某些时候所有这些在浏览器中也会失败(可能不是从本地主机提供服务)。
我正在考虑以某种方式获得系统注册的 promise 并制作类似于 HTMLImports.whenDocumentReady 的东西,但我仍然不清楚这一切是如何工作的。
非常感谢任何想法和建议!
示例应用程序在 github 上:
git clone https://github.com/bushuyev/test_test.git
cd test_test
git checkout tags/random_fail
npm install
bower install
gulp wct
为了让它成功多于失败 - 在 wct.conf.js 中将 verbose 更改为 true。
最佳答案
可以以非常好的方式一起使用 Polymer、SystemJS 和 TypeScript(类似于 ES6,但添加了对 Polymer 友好的语法),还可以使用 SystemJS 处理 HTML 导入。这确实涉及时间问题,我已经发布了 small shim首先等待 webcomponents.js 加载并捕获其就绪事件(在其他代码有机会看到它之前),然后它加载 Polymer,最后加载所有其他组件和 TypeScript 代码。然后它再次调度该事件,以便 Polymer 完成自身的初始化。
这是 an article about combining the technologies使用上述解决方案、可下载代码和演示。
关于javascript - 随机失败的聚合物网络组件测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31862834/