| 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>} */ |
| 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 |
| 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 == Node.ELEMENT_NODE) |
| 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) { |
| 92 if (this._instance) |
| 93 this._instance._notifyPath(path, value, true); |
| 94 } |
| 95 }); |
| OLD | NEW |