| 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"> |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 /** | 36 /** |
| 37 * @return {?Element} The element contained in the template, if it has | 37 * @return {?Element} The element contained in the template, if it has |
| 38 * already been stamped. | 38 * already been stamped. |
| 39 */ | 39 */ |
| 40 getIfExists: function() { | 40 getIfExists: function() { |
| 41 return this.child_; | 41 return this.child_; |
| 42 }, | 42 }, |
| 43 | 43 |
| 44 /** @private */ | 44 /** @private */ |
| 45 render_: function() { | 45 render_: function() { |
| 46 if (!this.ctor) | 46 // Only initialize once. |
| 47 this.templatize(this); | 47 assert(!this.ctor); |
| 48 // Must have a parent and child in order to add child to parent. |
| 49 assert(this.parentNode && !this.child_); |
| 50 this.templatize(this); |
| 48 var parentNode = this.parentNode; | 51 var parentNode = this.parentNode; |
| 49 if (parentNode && !this.child_) { | 52 var instance = this.stamp({}); |
| 50 var instance = this.stamp({}); | 53 this.child_ = instance.root.firstElementChild; |
| 51 this.child_ = instance.root.firstElementChild; | 54 parentNode.insertBefore(instance.root, this); |
| 52 parentNode.insertBefore(instance.root, this); | |
| 53 } | |
| 54 }, | 55 }, |
| 55 | 56 |
| 56 /** | 57 /** |
| 57 * @param {string} prop | 58 * @param {string} prop |
| 58 * @param {Object} value | 59 * @param {Object} value |
| 59 */ | 60 */ |
| 60 _forwardParentProp: function(prop, value) { | 61 _forwardParentProp: function(prop, value) { |
| 61 if (this.child_) | 62 if (this.child_) |
| 62 this.child_._templateInstance[prop] = value; | 63 this.child_._templateInstance[prop] = value; |
| 63 }, | 64 }, |
| 64 | 65 |
| 65 /** | 66 /** |
| 66 * @param {string} path | 67 * @param {string} path |
| 67 * @param {Object} value | 68 * @param {Object} value |
| 68 */ | 69 */ |
| 69 _forwardParentPath: function(path, value) { | 70 _forwardParentPath: function(path, value) { |
| 70 if (this.child_) | 71 if (this.child_) |
| 71 this.child_._templateInstance.notifyPath(path, value, true); | 72 this.child_._templateInstance.notifyPath(path, value, true); |
| 72 } | 73 } |
| 73 }); | 74 }); |
| OLD | NEW |