Skip to content

Commit 03bed19

Browse files
committed
Put $ on dom, and assign to element as needed. Eliminate _templateInfo reference.
1 parent 396c102 commit 03bed19

5 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎lib/elements/dom-bind.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
return;
8787
}
8888
this.root = this._stampBoundTemplate(template);
89+
this.$ = this.root.$;
8990
this.__children = [];
9091
for (let n=this.root.firstChild; n; n=n.nextSibling) {
9192
this.__children[this.__children.length] = n;

‎lib/mixins/element-mixin.html‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@
624624
hostStack.beginHosting(this);
625625
this.root = this._stampBoundTemplate(this._template);
626626
hostStack.endHosting(this);
627+
this.$ = this.root.$;
627628
}
628629
super.ready();
629630
}
@@ -747,6 +748,14 @@
747748
return Polymer.ResolveUrl.resolveUrl(url, base);
748749
}
749750

751+
/**
752+
* Overrides `PropertyAccessors` to add map of dynamic functions on
753+
* template info, for consumption by `PropertyEffects` template binding
754+
* code. This map determines which method templates should have accessors
755+
* created for them.
756+
*
757+
* @override
758+
*/
750759
static _parseTemplateContent(template, templateInfo, nodeInfo) {
751760
templateInfo.dynamicFns = templateInfo.dynamicFns || propertiesForClass(this);
752761
return super._parseTemplateContent(template, templateInfo, nodeInfo);

‎lib/mixins/property-effects.html‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@
11301130
* its prototype, the property effect lists will be cloned and added as
11311131
* own properties of the caller.
11321132
*
1133-
* @param {string} path Property that should trigger the effect
1133+
* @param {string} property Property that should trigger the effect
11341134
* @param {string} type Effect type, from this.PROPERTY_EFFECT_TYPES
11351135
* @param {Object=} effect Effect metadata object
11361136
* @protected
@@ -2015,12 +2015,23 @@
20152015
return this.__templateInfo = templateInfo;
20162016
}
20172017

2018-
static _addTemplatePropertyEffect(templateInfo, prop, info) {
2018+
/**
2019+
* Adds a property effect to the given template metadata, which is run
2020+
* at the "propagate" stage of `_propertiesChanged` when the template
2021+
* has been bound to the element via `_bindTemplate`.
2022+
*
2023+
* The `effect` object should match the format in `_addPropertyEffect`.
2024+
*
2025+
* @param {string} prop Property that should trigger the effect
2026+
* @param {Object=} effect Effect metadata object
2027+
* @protected
2028+
*/
2029+
static _addTemplatePropertyEffect(templateInfo, prop, effect) {
20192030
let hostProps = templateInfo.hostProps = templateInfo.hostProps || {};
20202031
hostProps[prop] = true;
20212032
let effects = templateInfo.propertyEffects = templateInfo.propertyEffects || {};
20222033
let propEffects = effects[prop] = effects[prop] || [];
2023-
propEffects.push(info);
2034+
propEffects.push(effect);
20242035
}
20252036

20262037
/**
@@ -2030,6 +2041,14 @@
20302041
* is returned containing the stamped DOM, ready for insertion into the
20312042
* DOM.
20322043
*
2044+
* This method may be called more than once; however note that due to
2045+
* `shadycss` polyfill limitations, only styles from templates prepared
2046+
* using `ShadyCSS.prepareTemplate` will be correctly polyfilled (scoped
2047+
* to the shadow root and support CSS custom properties), and note that
2048+
* `ShadyCSS.prepareTemplate` may only be called once per element. As such,
2049+
* any styles required by in runtime-stamped templates must be included
2050+
* in the main element template.
2051+
*
20332052
* @param {HTMLTemplateElement} template Template to stamp
20342053
* @return {DocumentFragment} Cloned template content
20352054
* @protected

‎lib/mixins/template-stamp.html‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@
381381
* The template is parsed (once and memoized) using this library's
382382
* template parsing features, and provides the following value-added
383383
* features:
384-
* * Adds declarative event listners for `on-event="handler"` attributes
385-
* * Generates an "id map" for all nodes with id's under `this.$`
384+
* * Adds declarative event listeners for `on-event="handler"` attributes
385+
* * Generates an "id map" for all nodes with id's under `$` on returned
386+
* document fragment
386387
* * Passes template info including `content` back to templates as
387388
* `_templateInfo` (a performance optimization to avoid deep template
388389
* cloning)
@@ -408,10 +409,10 @@
408409
// NOTE: ShadyDom optimization indicating there is an insertion point
409410
dom.__noInsertionPoint = !templateInfo.hasInsertionPoint;
410411
let nodes = dom.nodeList = new Array(nodeInfo.length);
411-
this.$ = {};
412+
dom.$ = {};
412413
for (let i=0, l=nodeInfo.length, info; (i<l) && (info=nodeInfo[i]); i++) {
413414
let node = nodes[i] = findTemplateNode(dom, info);
414-
applyIdToMap(this, this.$, node, info);
415+
applyIdToMap(this, dom.$, node, info);
415416
applyTemplateContent(this, node, info);
416417
applyEventListener(this, node, info);
417418
}

‎lib/utils/templatize.html‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
return templateHost && templateHost._methodHost || templateHost;
194194
}
195195

196-
function createTemplatizerClass(template, options) {
196+
function createTemplatizerClass(template, templateInfo, options) {
197197
// Anonymous class created by the templatize
198198
/**
199199
* @unrestricted
@@ -203,12 +203,11 @@
203203
let klass = class extends base { }
204204
klass.prototype.__templatizeOptions = options;
205205
klass.prototype._bindTemplate(template);
206-
addNotifyEffects(klass, template, options);
206+
addNotifyEffects(klass, template, templateInfo, options);
207207
return klass;
208208
}
209209

210-
function addPropagateEffects(template, options) {
211-
let templateInfo = template._templateInfo;
210+
function addPropagateEffects(template, templateInfo, options) {
212211
let userForwardHostProp = options.forwardHostProp;
213212
if (userForwardHostProp) {
214213
// Provide data API and property effects on memoized template class
@@ -250,8 +249,7 @@
250249
}
251250
}
252251

253-
function addNotifyEffects(klass, template, options) {
254-
let templateInfo = template._templateInfo;
252+
function addNotifyEffects(klass, template, templateInfo, options) {
255253
let hostProps = templateInfo.hostProps || {};
256254
for (let iprop in options.instanceProps) {
257255
delete hostProps[iprop];
@@ -381,26 +379,26 @@
381379
* @param {HTMLTemplateElement} template Template to templatize
382380
* @param {*} owner Owner of the template instances; any optional callbacks
383381
* will be bound to this owner.
384-
* @param {*} options Options dictionary (see summary for details)
382+
* @param {*=} options Options dictionary (see summary for details)
385383
* @return {TemplateInstanceBase} Generated class bound to the template
386384
* provided
387385
*/
388386
templatize(template, owner, options) {
387+
options = options || {};
389388
if (template.__templatizeOwner) {
390389
throw new Error('A <template> can only be templatized once');
391390
}
392391
template.__templatizeOwner = owner;
393-
let templateInfo = template._templateInfo;
392+
let templateInfo = owner.constructor._parseTemplate(template);
394393
// Get memoized base class for the prototypical template, which
395394
// includes property effects for binding template & forwarding
396-
let baseClass = templateInfo && templateInfo.templatizeInstanceClass;
395+
let baseClass = templateInfo.templatizeInstanceClass;
397396
if (!baseClass) {
398-
baseClass = createTemplatizerClass(template, options);
399-
templateInfo = template._templateInfo;
397+
baseClass = createTemplatizerClass(template, templateInfo, options);
400398
templateInfo.templatizeInstanceClass = baseClass;
401399
}
402400
// Host property forwarding must be installed onto template instance
403-
addPropagateEffects(template, options);
401+
addPropagateEffects(template, templateInfo, options);
404402
// Subclass base class and add reference for this specific template
405403
let klass = class TemplateInstance extends baseClass {};
406404
klass.prototype._methodHost = findMethodHost(template);

0 commit comments

Comments
 (0)