javascript - PolymerJS 中的 bind() 和 bindProperty() 有什么区别?

标签 javascript data-binding polymer

似乎 bind() 是 Web Components 规范的一部分,Polymer 出于各种原因对其进行了扩展 with Node.bind() .

这是 docs on bindProperty() .这只是 bind() 的内部实现/polyfill 吗?因此,开发人员是否应该使用 bind() 而不是 bindProperty()

最佳答案

答案在bind的源代码中-line 56 (下面的完整片段)。 bind 函数调用内部 bindProperty 函数。 bindbindProperty 之上所做的一切都是确保给定的属性存在。

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,但我不会推荐它,除非您可以确保要绑定(bind)的属性的存在。

关于javascript - PolymerJS 中的 bind() 和 bindProperty() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882773/

相关文章:

javascript - 主干 subview 定义 - 主视图与路由器

javascript - javascript中的 chop 单词函数(研究dojo的代码)

javascript - 是的,当 .required() 为 false 时,会继续在同一个属性上运行验证器

WPF - 绑定(bind)到 Oracle 数据库

javascript - 声明函数触发 Polymer 元素中的事件的正确方法是什么?

javascript - 如何在 Polymer 项目中加载 Facebook Javascript sdk?

dart - 在 polymer 表中迭代时将文本渲染为 html

javascript - Array.prototype 方法会影响函数中的多个变量吗?

http - 使用Grails,如何在不同的请求/ Controller 操作之间保留信息

c# - 为什么 Container.DataItem 作为字符串文字传递?