强制绑定

Binding imperatively

有没有办法强制设置绑定。示例用例:

var el2 = new MyElement();
el2.myProp = this.$.anotherElement.anotherProp

这不会设置绑定,它只是分配值或对象。我想找到一种方法来做类似的事情:

el2.myProp.bindTo(this.$.anotherElement.anotherProp)

可能吗?

Polymer 1.0 目前不支持此功能 - 正如@kevinpschaaf 在 Github https://github.com/Polymer/polymer/issues/1778.

中所解释的那样

(comment by @kevinpschaaf)

No, we don't currently support this, outside of dom-bind, which is the only template implementation that late-binds instance children. You can document.createElement('template', 'dom-bind'), then you can dynamically append children with binding annotations to its content, and the bindings will only be evaluated once the dom-bind is attached to the document. See tests here that show this usage of it: https://github.com/Polymer/polymer/blob/master/test/unit/dom-bind.html#L95

Note that dom-bind does not currently allow binding to outer scope, so it has limited use in custom element templates (it's main use case is for binding between elements in the main document), and that's not likely to change short-term.

We are achieving a lot of performance optimization by baking the binding connections into the prototype at registration time for an element (rather than at instance time), and we haven't built up enough of the machinery to easily allow runtime addition/removal of bindings.