Skip to content

Commit

Permalink
Merge pull request #2004 from Polymer/2003-kschaaf-model-fire
Browse files Browse the repository at this point in the history
Move modelForElement to Templatizer, use Polymer.Base.fire in Bind lib. Fixes #2003
  • Loading branch information
Steve Orvell committed Jul 6, 2015
2 parents ef82dbc + 573cb37 commit 5beaf03
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
11 changes: 3 additions & 8 deletions src/lib/bind/accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
prepareModel: function(model) {
model._propertyEffects = {};
model._bindListeners = [];
// TODO(sjmiles): no mixin function?
var api = this._modelApi;
for (var n in api) {
model[n] = api[n];
}
Polymer.Base.mixin(model, this._modelApi);
},

_modelApi: {

_notifyChange: function(property) {
var eventName = Polymer.CaseMap.camelToDashCase(property) + '-changed';
// TODO(sjmiles): oops, `fire` doesn't exist at this layer
this.fire(eventName, {
Polymer.Base.fire(eventName, {
value: this[property]
}, {bubbles: false});
}, {bubbles: false, node: this});
},

// TODO(sjmiles): removing _notifyListener from here breaks accessors.html
Expand Down
40 changes: 0 additions & 40 deletions src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -604,46 +604,6 @@
}
},

/**
* Returns the template "model" associated with a given element, which
* serves as the binding scope for the template instance the element is
* contained in. A template model is an instance of `Polymer.Base`, and
* should be used to manipulate data associated with this template instance.
*
* Example:
*
* var model = modelForElement(el);
* if (model.index < 10) {
* model.set('item.checked', true);
* }
*
* @method modelForElement
* @param {HTMLElement} el Element for which to return a template model.
* @return {Object<Polymer.Base>} Model representing the binding scope for
* the element.
*/
modelForElement: function(el) {
var model;
while (el) {
// An element with a _templateInstance marks the top boundary
// of a scope; walk up until we find one, and then ensure that
// its dataHost matches `this`, meaning this dom-repeat stamped it
if (model = el._templateInstance) {
// Found an element stamped by another template; keep walking up
// from its dataHost
if (model.dataHost != this) {
el = model.dataHost;
} else {
return model;
}
} else {
// Still in a template scope, keep going up until
// a _templateInstance is found
el = el.parentNode;
}
}
},

/**
* Returns the item associated with a given element stamped by
* this `dom-repeat`.
Expand Down
40 changes: 40 additions & 0 deletions src/lib/template/templatizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,46 @@
}
}
return new this.ctor(model, this);
},

/**
* Returns the template "model" associated with a given element, which
* serves as the binding scope for the template instance the element is
* contained in. A template model is an instance of `Polymer.Base`, and
* should be used to manipulate data associated with this template instance.
*
* Example:
*
* var model = modelForElement(el);
* if (model.index < 10) {
* model.set('item.checked', true);
* }
*
* @method modelForElement
* @param {HTMLElement} el Element for which to return a template model.
* @return {Object<Polymer.Base>} Model representing the binding scope for
* the element.
*/
modelForElement: function(el) {
var model;
while (el) {
// An element with a _templateInstance marks the top boundary
// of a scope; walk up until we find one, and then ensure that
// its dataHost matches `this`, meaning this dom-repeat stamped it
if (model = el._templateInstance) {
// Found an element stamped by another template; keep walking up
// from its dataHost
if (model.dataHost != this) {
el = model.dataHost;
} else {
return model;
}
} else {
// Still in a template scope, keep going up until
// a _templateInstance is found
el = el.parentNode;
}
}
}

// TODO(sorvell): note, using the template as host is ~5-10% faster if
Expand Down

0 comments on commit 5beaf03

Please sign in to comment.