javascript - 随机失败的聚合物网络组件测试

标签 javascript polymer-1.0 systemjs es6-module-loader

又一次尝试将 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。

更新种类:How to import system js SFX library

最佳答案

可以以非常好的方式一起使用 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/

相关文章:

javascript - 后台提交表单,后台使用信息

javascript - Javascript 中的 Linting Promise

javascript - 如何在一些代码后导入ES6(SystemJS)?

javascript - 使用 defaultExtension 为 systemjs 配置 Angular2 库

PHP - 动态 CSS/JS 是否被浏览器缓存?

javascript - 带选择器的 jQuery appendTo()

javascript - 在输入标签中的 onchange 事件上调用聚合函数

javascript - ie11 的 Polymer 内容问题

javascript - 如何在 polymer 中的另一个属性中传递属性值?

typescript - 在 Angular 2 typescript 应用程序中使用 moment.js