【问题标题】:What's the difference between bind() and bindProperty() in PolymerJS?PolymerJS 中的 bind() 和 bindProperty() 有什么区别?
【发布时间】:2015-03-09 02:02:30
【问题描述】:

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

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

【问题讨论】:

    标签: 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,但除非你能确保你想要绑定的属性存在,否则我不会推荐它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2010-09-20
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      相关资源
      最近更新 更多