Eric Nguyen
Eric Nguyen

Reputation: 40582

What's the difference between bind() and bindProperty() in PolymerJS?

Seems like bind() is part of the Web Components spec, which Polymer extends for various reasons with Node.bind().

Here's docs on bindProperty(). Is this simply the internal implementation/polyfill for bind()? And thus should developers thus be using bind() rather than bindProperty()?

Upvotes: 1

Views: 315

Answers (1)

user2210287
user2210287

Reputation:

The answer is in the source code of bind - line 56(full snippet below). There the bind function calls the internal bindProperty function. All that bind is doing on top of bindProperty is ensuring that the given property exists.

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;
}

So, you could use bindProperty, but I would not recommend it unless you can ensure the existence of the property, which you want to bind.

Upvotes: 4

Related Questions