Can dynamically created Local/Light DOM in Polymer be processed to ensure correct bindings?

為{幸葍}努か 提交于 2019-12-01 06:49:58
01es

It appears that for now Polymer 1.0 does not support dynamic (imperative) bindings. Here and here are the relevant discussions.

The information from the above links suggests a reliable, albeit limited, approach to creation of dynamic content with bindings by means of using dom-bind template elements. The limitation is due to bindings working strictly with locally defined methods and properties for the dom-bind instance being created.

Here is an example, where domBind is created imperatively with properties tapCount, tapMessage and method _tapMe. The latter is used as on-tap event handler for the added to the domBind element div.

attached: function () {
   var domBind = document.createElement('template', 'dom-bind');
   domBind.tapCount = 0;
   domBind.tapMessage = 'Tap me now!';
   domBind._tapMe = function(e) {
                this.tapCount = this.tapCount + 1;
                this.tapMessage = 'Tapped ' + this.tapCount + ' time(s).';
   };
   var div = domBind.content.ownerDocument.createElement('div');
   div.innerHTML = '<b on-tap="_tapMe">[[tapMessage]]</b>';
   domBind.content.appendChild(div);
   Polymer.dom(this.$.container).appendChild(domBind);
   Polymer.dom.flush();
}

A similar to my question is posted here, which helped me to get a better appreciation for the scope of the problem (especially with the reference to Polymer 0.5 injectBoundHTML).


Of course, external methods and properties can still be accessed:

...
var self = this;
domBind.externalMethod = function() {
    return self._computeValue();
};
...

Then by binding locally defined externalMethod one can "bind" things from the "external" scope. In this case, _computeValue refers to a method defined on a custom element with imperative light DOM construction happening in its method attached.

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