Does Polymers template auto-binding break model property?

五迷三道 提交于 2019-12-04 20:27:16

The best set of docs for <template is="auto-binding"> are at http://www.polymer-project.org/docs/polymer/databinding-advanced.html#autobinding

The examples there illustrate setting the bound variables on the <template> directly, rather than going through the model. It's what you would do if you were using the template from within a Polymer element, and I believe the idea is to maintain that usage pattern rather than interacting with the model like you would with a non-auto-binding <template>.

That being said, if you do want to set the model, what seems to work is to do so after the template-bound event is fired—your setTimeout() was approximating that, but it's obviously cleaner to just listen for that event. Here's an example (jsbin):

  var template = document.getElementById("root");
  template.addEventListener('template-bound', function() {
    template.model = {
      "list": [
        {"desc": "first"},
        {"desc": "second"},
        {"desc": "third"}
      ]
    };
  });

EDIT: Looking at the relevant source code, it appears that ignoring the model property of the <template> initially is intentional, and it gives some insight as to why setting model after template-bound works if you really want to do that.

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