OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * cr-lazy-render is a simple variant of dom-if designed for lazy rendering | 7 * cr-lazy-render is a simple variant of dom-if designed for lazy rendering |
8 * of elements that are accessed imperatively. | 8 * of elements that are accessed imperatively. |
9 * Usage: | 9 * Usage: |
10 * <template is="cr-lazy-render" id="menu"> | 10 * <template is="cr-lazy-render" id="menu"> |
11 * <heavy-menu></heavy-menu> | 11 * <heavy-menu></heavy-menu> |
12 * </template> | 12 * </template> |
13 * | 13 * |
14 * this.$.menu.get().show(); | 14 * this.$.menu.get().show(); |
15 */ | 15 */ |
16 | 16 |
17 Polymer({ | 17 Polymer({ |
18 is: 'cr-lazy-render', | 18 is: 'cr-lazy-render', |
19 extends: 'template', | 19 extends: 'template', |
20 | 20 |
21 behaviors: [ | 21 behaviors: [Polymer.Templatizer], |
22 Polymer.Templatizer | |
23 ], | |
24 | 22 |
25 /** @private {TemplatizerNode} */ | 23 /** @private {TemplatizerNode} */ |
26 child_: null, | 24 child_: null, |
27 | 25 |
28 /** | 26 /** |
29 * Stamp the template into the DOM tree synchronously | 27 * Stamp the template into the DOM tree synchronously |
30 * @return {Element} Child element which has been stamped into the DOM tree. | 28 * @return {Element} Child element which has been stamped into the DOM tree. |
31 */ | 29 */ |
32 get: function() { | 30 get: function() { |
33 if (!this.child_) | 31 if (!this.child_) |
34 this.render_(); | 32 this.render_(); |
35 return this.child_; | 33 return this.child_; |
36 }, | 34 }, |
37 | 35 |
38 /** | 36 /** |
39 * @return {?Element} The element contained in the template, if it has | 37 * @return {?Element} The element contained in the template, if it has |
40 * already been stamped. | 38 * already been stamped. |
41 */ | 39 */ |
42 getIfExists: function() { | 40 getIfExists: function() { return this.child_; }, |
43 return this.child_; | |
44 }, | |
45 | 41 |
46 /** @private */ | 42 /** @private */ |
47 render_: function() { | 43 render_: function() { |
48 if (!this.ctor) | 44 if (!this.ctor) |
49 this.templatize(this); | 45 this.templatize(this); |
50 var parentNode = this.parentNode; | 46 var parentNode = this.parentNode; |
51 if (parentNode && !this.child_) { | 47 if (parentNode && !this.child_) { |
52 var instance = this.stamp({}); | 48 var instance = this.stamp({}); |
53 this.child_ = instance.root.firstElementChild; | 49 this.child_ = instance.root.firstElementChild; |
54 parentNode.insertBefore(instance.root, this); | 50 parentNode.insertBefore(instance.root, this); |
(...skipping 11 matching lines...) Expand all Loading... |
66 | 62 |
67 /** | 63 /** |
68 * @param {string} path | 64 * @param {string} path |
69 * @param {Object} value | 65 * @param {Object} value |
70 */ | 66 */ |
71 _forwardParentPath: function(path, value) { | 67 _forwardParentPath: function(path, value) { |
72 if (this.child_) | 68 if (this.child_) |
73 this.child_._templateInstance.notifyPath(path, value, true); | 69 this.child_._templateInstance.notifyPath(path, value, true); |
74 } | 70 } |
75 }); | 71 }); |
OLD | NEW |