Add get() parentModel and parentModel option; privatize __templateInstance

This commit is contained in:
Kevin Schaaf 2017-02-27 11:27:31 -08:00
parent 6027647a69
commit 91457ef131
5 changed files with 37 additions and 8 deletions

View File

@ -315,6 +315,7 @@ Then the `observe` property should be configured as follows:
instanceProps[this.indexAs] = true;
instanceProps[this.itemsIndexAs] = true;
this.__ctor = Polymer.Templatize.templatize(template, this, {
parentModel: true,
instanceProps: instanceProps,
forwardHostProp: function(prop, value) {
var i$ = this.__instances;

View File

@ -18,6 +18,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
templatize(template) {
this._templatizerTemplate = template;
this.ctor = Polymer.Templatize.templatize(template, this, {
parentModel: this._parentModel,
instanceProps: this._instanceProps,
forwardHostProp: this._forwardHostPropV2,
notifyInstanceProp: this._notifyInstancePropV2

View File

@ -47,7 +47,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
let children = this.children = [];
for (let n = this.root.firstChild; n; n=n.nextSibling) {
children.push(n);
n._templateInstance = this;
n.__templatizeInstance = this;
}
if (this.__templatizeOwner.__hideTemplateChildren__) {
this._showHideChildren(true);
@ -78,9 +78,9 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}
}
_addEventListenerToNode(node, eventName, handler) {
if (this._methodHost && this.__templatizeOptions.instanceProps) {
// If this instance adds instance props, decorate events with
// `model` as this template instance
if (this._methodHost && this.__templatizeOptions.parentModel) {
// If this instance should be considered a parent model, decorate
// events this template instance as `model`
this._methodHost._addEventListenerToNode(node, eventName, (e) => {
e.model = this;
handler(e);
@ -137,6 +137,27 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
super._setUnmanagedPropertyToNode(node, prop, value);
}
}
/**
* Find the parent model of this template instance. The parent model
* is either another templatize instance that had option `parentModel: true`,
* or else the host element.
*
* @return {Polymer.PropertyEffectsInterface} The parent model of this instance
*/
get parentModel() {
let model = this.__parentModel;
if (!model) {
let options;
model = this
do {
// A template instance's `__dataHost` is a <template>
// `model.__dataHost.__dataHost` is the template's host
model = model.__dataHost.__dataHost;
} while ((options = model.__templatizeOptions) && !options.parentModel)
this.__parentModel = model;
}
return model;
}
}
function findMethodHost(template) {
@ -281,10 +302,10 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
modelForElement(host, el) {
let model;
while (el) {
// An element with a _templateInstance marks the top boundary
// An element with a __templatizeInstance 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)) {
if ((model = el.__templatizeInstance)) {
// Found an element stamped by another template; keep walking up
// from its __dataHost
if (model.__dataHost != host) {
@ -294,7 +315,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}
} else {
// Still in a template scope, keep going up until
// a _templateInstance is found
// a __templatizeInstance is found
el = el.parentNode;
}
}

View File

@ -5059,7 +5059,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
assert.equal(model.itemc.prop, 'prop-1-1-3');
});
test('event.model', function() {
test('event.model && parentModel', function() {
let el = document.createElement('x-repeat-with-if');
document.body.appendChild(el);
Polymer.flush();
@ -5075,6 +5075,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
assert.equal(event._target, buttons[0]);
assert.equal(event.model.item, el.items[0].items[0]);
assert.equal(event.model.item.prop, 'a');
assert.equal(event.model.parentModel.item, el.items[0]);
assert.equal(outer.modelForElement(event._target).item, el.items[0]);
// Second
@ -5085,6 +5086,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
assert.equal(event._target, buttons[1]);
assert.equal(event.model.item, el.items[0].items[1]);
assert.equal(event.model.item.prop, 'b');
assert.equal(event.model.parentModel.item, el.items[0]);
assert.equal(outer.modelForElement(event._target).item, el.items[0]);
// Third
@ -5095,6 +5097,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
assert.equal(event._target, buttons[2]);
assert.equal(event.model.item, el.items[1].items[0]);
assert.equal(event.model.item.prop, 'c');
assert.equal(event.model.parentModel.item, el.items[1]);
assert.equal(outer.modelForElement(event._target).item, el.items[1]);
// Fourth
@ -5105,6 +5108,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
assert.equal(event._target, buttons[3]);
assert.equal(event.model.item, el.items[1].items[1]);
assert.equal(event.model.item.prop, 'd');
assert.equal(event.model.parentModel.item, el.items[1]);
assert.equal(outer.modelForElement(event._target).item, el.items[1]);
document.body.removeChild(el);

View File

@ -102,6 +102,7 @@
go: function(withProps) {
var template = this.querySelector('template');
var ctor = Polymer.Templatize.templatize(template, this, {
parentModel: true,
instanceProps: {
obj: true,
prop: true,
@ -154,6 +155,7 @@
this.instance.notifyPath(info.path, info.value);
}
},
_parentModel: true,
_instanceProps: {
obj: true,
prop: true,