强制绑定 [英] Binding imperatively

查看:19
本文介绍了强制绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制设置绑定.一个示例用例:

Is there a way to set up bindings imperatively. An example use case:

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

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

That won't setup a binding, it just assigns the value or object. I'd like to find a way to do something like:

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

可能吗?

推荐答案

Polymer 1.0 目前不支持这一点 - 正如 @kevinpschaaf 在 Github https://github.com/Polymer/polymer/issues/1778.

Polymer 1.0 does not support this at the moment - as explained by @kevinpschaaf in Github https://github.com/Polymer/polymer/issues/1778.

(@kevinpschaaf 评论)

(comment by @kevinpschaaf)

不,我们目前不支持这个,除了 dom-bind,这是只有后期绑定实例子项的模板实现.你可以 document.createElement('template', 'dom-bind'),那么你就可以动态地将具有绑定注释的子项附加到其内容中,并且仅在附加 dom-bind 后才会评估绑定到文档.请参阅此处的测试以显示它的这种用法:https://github.com/Polymer/polymer/blob/master/test/unit/dom-bind.html#L95

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

注意 dom-bind 目前不允许绑定到外部作用域,所以它在自定义元素模板中的使用有限(它的主要用例是用于在主文档中的元素之间进行绑定),这不是可能会在短期内发生变化.

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.

这篇关于强制绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆