Polymer + requirejs: Attributes on were data bound prior to Polymer upgrading the element

杀马特。学长 韩版系。学妹 提交于 2019-11-29 08:46:19

EDIT: Thanks for clarifying that this only is an issue when your Polymer element is inside another Polymer element. That points to the underlying issue. It's not particular to using requirejs; you can reproduce the warning via something like

<script>
  setTimeout(function() {
    Polymer('test-b', {});
  }, 0);
</script>

In both cases, the Polymer('test-b', ...) function is invoked asynchronously after the parent element (<test-a> in your example) has already been fully initialized, and that's apparently not a good practice.

You could refactor things a bit to get to something that Polymer is happy with. For instance, if module holds a bunch of properties that you want to set on your <test-b>, you could defer loading the module and setting them until after the created callback has been fired, like so:

<script>
  Polymer('test-b', {
    created: function() {
      var testB = this;
      require(['test-b-js'], function (module) {
        // There might be a more efficient way to copy over the properties...
        Object.keys(module).forEach(function(key) {
          testB.publish[key] = module[key];
        });
      }); 
    }
  });
</script>

This also follows the documented best practice for initializing properties that are array/objects without having to worry about shared state gotchas, so there's an added benefit. There is the slight concern that the properties won't actually be set during the created callback but instead slightly afterwards (since, again, the callback in require() is asynchronous), but in practice that might not be an issue.

A potentially larger concern is that those properties won't be published; if that's important to you, then a more creative solution is needed.

The discussion in Jeff's answer is right on the money. Once you defer the registration of an element in some way (e.g. setTimeout, require) you have to make sure nobody else tries to use the element before it is ready.

Another possibility is to only defer access to the module code and not block the element entirely, something like this:

<script>
(function() {

  var base = null;
  require(['base'], function(module) {
    base = module;
  });

  Polymer({
    get name() {
      return base ? null : base.name; 
    }
  });

})();
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!