3

又一次尝试将 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
 }
});

这似乎在浏览器中工作得相当稳定,至少在从 localhost 加载时是这样。但唯一“修复”测试的是延迟 Polymer 的就绪调用:

Polymer.whenReady = function (f) {
   console.log("polymer ready");
   setTimeout(f, 100);// "fix"
   //f();
}

这意味着在某些时候,所有这些都将在浏览器中失败(可能不是从本地主机提供服务时)。

我正在考虑以某种方式获得系统注册的承诺并制作类似于 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。

更新类型:如何导入系统 js SFX 库

4

1 回答 1

1

可以以非常好的方式一起使用 Polymer、SystemJS 和 TypeScript(如 ES6,但添加了对 Polymer 友好的语法),还可以使用 SystemJS 处理 HTML 导入。这确实涉及时间问题,我已经发布了一个小垫片,它首先等待 webcomponents.js 加载并捕获其就绪事件(在其他代码有机会看到它之前),然后加载 Polymer,最后加载所有其他组件和 TypeScript 代码。然后它再次分派事件,这样 Polymer 就完成了自身的初始化。

是一篇关于将技术与上述解决方案、可下载代码和演示相结合的文章。

于 2015-08-27T16:17:35.340 回答