【问题标题】:What's the difference between bind() and bindProperty() in PolymerJS?PolymerJS 中的 bind() 和 bindProperty() 有什么区别?
【发布时间】:2015-03-09 02:02:30
【问题描述】:
【问题讨论】:
标签:
javascript
data-binding
polymer
【解决方案1】:
答案在bind-line 56的源代码中(下面是完整的sn-p)。那里bind 函数调用内部bindProperty 函数。 bind 在 bindProperty 之上所做的所有事情都是确保给定的属性存在。
bind: function(name, observable, oneTime) {
var property = this.propertyForAttribute(name);
if (!property) {
// TODO(sjmiles): this mixin method must use the special form
// of `super` installed by `mixinMethod` in declaration/prototype.js
return this.mixinSuper(arguments);
} else {
// use n-way Polymer binding
var observer = this.bindProperty(property, observable, oneTime);
// NOTE: reflecting binding information is typically required only for
// tooling. It has a performance cost so it's opt-in in Node.bind.
if (Platform.enableBindingsReflection && observer) {
observer.path = observable.path_;
this._recordBinding(property, observer);
}
if (this.reflect[property]) {
this.reflectPropertyToAttribute(property);
}
return observer;
}
所以,你可以使用bindProperty,但除非你能确保你想要绑定的属性存在,否则我不会推荐它。