From 573cb3786daf1a0df6ac2a2d0e9408e4acbb5ad6 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Wed, 1 Jul 2015 10:24:37 -0700 Subject: [PATCH] Move modelForElement to Templatizer, use Polymer.Base.fire in Bind lib. Fixes #2003 --- src/lib/bind/accessors.html | 11 +++------ src/lib/template/dom-repeat.html | 40 ------------------------------- src/lib/template/templatizer.html | 40 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/lib/bind/accessors.html b/src/lib/bind/accessors.html index e3b5537c5d..7a9f96ed2f 100644 --- a/src/lib/bind/accessors.html +++ b/src/lib/bind/accessors.html @@ -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 diff --git a/src/lib/template/dom-repeat.html b/src/lib/template/dom-repeat.html index 88795a0845..5494b24287 100644 --- a/src/lib/template/dom-repeat.html +++ b/src/lib/template/dom-repeat.html @@ -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} 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`. diff --git a/src/lib/template/templatizer.html b/src/lib/template/templatizer.html index a8ecbf881b..edd499ecd0 100644 --- a/src/lib/template/templatizer.html +++ b/src/lib/template/templatizer.html @@ -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} 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