Skip to content

Commit 10d657b

Browse files
committed
Back to single template getter. Add more comments.
1 parent c4b94a0 commit 10d657b

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

lib/legacy/class.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,6 @@ function GenerateClassFromInfo(info, Base) {
148148
return info.observers;
149149
}
150150

151-
/**
152-
* @return {HTMLTemplateElement} template for this class
153-
*/
154-
static get template() {
155-
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
156-
this._template =
157-
// Accept template: _null to short-circuit dom-module lookup
158-
info._template !== undefined ? info._template :
159-
// Look in dom-module associated with this element's is
160-
this._getTemplateFromDomModule() ||
161-
// Look up in the chain
162-
Base.template ||
163-
// Use any template set on the prototype via registered callback
164-
this.prototype._template;
165-
}
166-
return this._template;
167-
}
168-
169151
/**
170152
* @return {void}
171153
*/

lib/mixins/element-mixin.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,29 @@ export const ElementMixin = dedupingMixin(base => {
270270
}
271271
}
272272

273+
/**
274+
* Look up template from dom-module for element
275+
*
276+
* @param {!string} is Element name to look up
277+
* @return {!HTMLTemplateElement} Template found in dom module, or
278+
* undefined if not found
279+
* @protected
280+
*/
281+
function getTemplateFromDomModule(is) {
282+
let template = null;
283+
// Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
284+
// when opted-in via allowTemplateFromDomModule
285+
if (is && (!strictTemplatePolicy || allowTemplateFromDomModule)) {
286+
template = DomModule.import(is, 'template');
287+
// Under strictTemplatePolicy, require any element with an `is`
288+
// specified to have a dom-module
289+
if (strictTemplatePolicy && !template) {
290+
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${is}`);
291+
}
292+
}
293+
return template;
294+
}
295+
273296
/**
274297
* @polymer
275298
* @mixinClass
@@ -341,30 +364,6 @@ export const ElementMixin = dedupingMixin(base => {
341364
}
342365
}
343366

344-
/**
345-
* Look up template from dom-module for element
346-
*
347-
* @param {!string} is Element name to look up
348-
* @return {!HTMLTemplateElement} Template found in dom module, or
349-
* undefined if not found
350-
* @protected
351-
*/
352-
static _getTemplateFromDomModule() {
353-
let template = null;
354-
const is = /** @type {PolymerElementConstructor}*/ (this).is;
355-
// Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
356-
// when opted-in via allowTemplateFromDomModule
357-
if (is && (!strictTemplatePolicy || allowTemplateFromDomModule)) {
358-
template = DomModule.import(is, 'template');
359-
// Under strictTemplatePolicy, require any element with an `is`
360-
// specified to have a dom-module
361-
if (strictTemplatePolicy && !template) {
362-
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${is}`);
363-
}
364-
}
365-
return template;
366-
}
367-
368367
/**
369368
* Returns the template that will be stamped into this element's shadow root.
370369
*
@@ -402,13 +401,30 @@ export const ElementMixin = dedupingMixin(base => {
402401
* @return {HTMLTemplateElement|string} Template to be stamped
403402
*/
404403
static get template() {
404+
// Explanation of template-related properties:
405+
// - constructor.template (this getter): the template for the class.
406+
// This can come from the prototype (for legacy elements), from a
407+
// dom-module, or from the super class's template (or can be overridden
408+
// altogether by the user)
409+
// - constructor._template: memoized version of constructor.template
410+
// - prototype._template: working template for the element, which will be
411+
// parsed and modified in place. It is a cloned version of
412+
// constructor.template, saved in _finalizeClass(). Note that before
413+
// this getter is called, for legacy elements this could be from a
414+
// _template field on the info object passed to Polymer(), a behavior,
415+
// or set in registered(); once the static getter runs, a clone of it
416+
// will overwrite it on the prototype as the working template.
405417
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
406418
this._template =
419+
// If user has put template on prototype (e.g. in legacy via registered
420+
// callback or info object), prefer that first
421+
this.prototype.hasOwnProperty(JSCompiler_renameProperty('_template', this.prototype)) ?
422+
this.prototype._template :
407423
// Look in dom-module associated with this element's is
408-
this._getTemplateFromDomModule() ||
424+
(getTemplateFromDomModule(/** @type {PolymerElementConstructor}*/ (this).is) ||
409425
// Next look for superclass template (call the super impl this
410426
// way so that `this` points to the superclass)
411-
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template;
427+
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template);
412428
}
413429
return this._template;
414430
}

0 commit comments

Comments
 (0)