Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * history-lazy-render is a simple variant of dom-if designed for lazy rendering | |
| 8 * of elements that are accessed imperatively. | |
| 9 * Usage: | |
| 10 * <template is="history-lazy-render" id="menu"> | |
| 11 * <heavy-menu></heavy-menu> | |
| 12 * </template> | |
| 13 * | |
| 14 * this.$.menu.get().then(function(menu) { | |
| 15 * menu.show(); | |
| 16 * }); | |
| 17 */ | |
| 18 | |
| 19 Polymer({ | |
| 20 is: 'history-lazy-render', | |
| 21 extends: 'template', | |
| 22 | |
| 23 behaviors: [ | |
| 24 Polymer.Templatizer | |
| 25 ], | |
| 26 | |
| 27 /** @private {Promise<Element>} */ | |
|
calamity
2016/08/12 06:25:04
Is this a valid type annotation? And below.
tsergeant
2016/08/16 03:49:06
Yup. We use it somewhere else as well. See https:/
calamity
2016/08/16 07:00:01
Acknowledged.
| |
| 28 _renderPromise: null, | |
| 29 | |
| 30 /** @private {TemplateInstance} */ | |
| 31 _instance: null, | |
| 32 | |
| 33 /** | |
| 34 * Stamp the template into the DOM tree asynchronously | |
| 35 * @return {Promise<Element>} Promise which resolves when the template has | |
| 36 * been stamped. | |
| 37 */ | |
| 38 get: function() { | |
| 39 if (!this._renderPromise) { | |
| 40 this._renderPromise = new Promise(function(resolve) { | |
| 41 this._debounceTemplate(function() { | |
| 42 this._render(); | |
| 43 this._renderPromise = null; | |
| 44 resolve(this.getIfExists()); | |
| 45 }.bind(this)); | |
| 46 }.bind(this)); | |
| 47 } | |
| 48 return this._renderPromise; | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * @return {Element} The element contained in the template, if it has | |
|
calamity
2016/08/12 06:25:04
I'm surprised this lets you return null without us
tsergeant
2016/08/16 03:49:06
Everything is nullable by default, so that's why t
| |
| 53 * already been stamped. | |
| 54 */ | |
| 55 getIfExists: function() { | |
| 56 if (this._instance) { | |
| 57 var children = this._instance._children; | |
| 58 | |
| 59 for (var i = 0; i < children.length; i++) { | |
| 60 if (children[i].nodeType == 1) | |
|
calamity
2016/08/12 06:25:04
== Node.ELEMENT_NODE?
tsergeant
2016/08/16 03:49:06
Huh. I should have known something like this would
| |
| 61 return children[i]; | |
| 62 } | |
| 63 } | |
| 64 return null; | |
| 65 }, | |
| 66 | |
| 67 _render: function() { | |
| 68 if (!this.ctor) | |
| 69 this.templatize(this); | |
| 70 var parentNode = this.parentNode; | |
| 71 if (parentNode && !this._instance) { | |
| 72 this._instance = /** @type {TemplateInstance} */(this.stamp({})); | |
| 73 var root = this._instance.root; | |
| 74 parentNode.insertBefore(root, this); | |
| 75 } | |
| 76 }, | |
| 77 | |
| 78 /** | |
| 79 * @param {string} prop | |
| 80 * @param {Object} value | |
| 81 */ | |
| 82 _forwardParentProp: function(prop, value) { | |
| 83 if (this._instance) | |
| 84 this._instance.__setProperty(prop, value, true); | |
| 85 }, | |
| 86 | |
| 87 /** | |
| 88 * @param {string} path | |
| 89 * @param {Object} value | |
| 90 */ | |
| 91 _forwardParentPath: function(path, value) { | |
|
calamity
2016/08/12 06:25:04
So these all depend on Polymer's internal implemen
tsergeant
2016/08/16 03:49:06
Yeah, seems like this is the only way to use Polym
| |
| 92 if (this._instance) | |
| 93 this._instance._notifyPath(path, value, true); | |
| 94 } | |
| 95 }); | |
| OLD | NEW |