Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2488)

Unified Diff: chrome/browser/resources/md_history/lazy_render.js

Issue 2230003002: MD History: Add lazy-render element for simple lazy initialization. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/md_history/lazy_render.js
diff --git a/chrome/browser/resources/md_history/lazy_render.js b/chrome/browser/resources/md_history/lazy_render.js
new file mode 100644
index 0000000000000000000000000000000000000000..b2744edaa050579ef35a253dda103e85591ac620
--- /dev/null
+++ b/chrome/browser/resources/md_history/lazy_render.js
@@ -0,0 +1,95 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview
+ * history-lazy-render is a simple variant of dom-if designed for lazy rendering
+ * of elements that are accessed imperatively.
+ * Usage:
+ * <template is="history-lazy-render" id="menu">
+ * <heavy-menu></heavy-menu>
+ * </template>
+ *
+ * this.$.menu.get().then(function(menu) {
+ * menu.show();
+ * });
+ */
+
+Polymer({
+ is: 'history-lazy-render',
+ extends: 'template',
+
+ behaviors: [
+ Polymer.Templatizer
+ ],
+
+ /** @private {Promise<Element>} */
+ _renderPromise: null,
+
+ /** @private {TemplateInstance} */
+ _instance: null,
+
+ /**
+ * Stamp the template into the DOM tree asynchronously
+ * @return {Promise<Element>} Promise which resolves when the template has
+ * been stamped.
+ */
+ get: function() {
+ if (!this._renderPromise) {
+ this._renderPromise = new Promise(function(resolve) {
+ this._debounceTemplate(function() {
+ this._render();
+ this._renderPromise = null;
+ resolve(this.getIfExists());
+ }.bind(this));
+ }.bind(this));
+ }
+ return this._renderPromise;
+ },
+
+ /**
+ * @return {?Element} The element contained in the template, if it has
+ * already been stamped.
+ */
+ getIfExists: function() {
+ if (this._instance) {
+ var children = this._instance._children;
+
+ for (var i = 0; i < children.length; i++) {
+ if (children[i].nodeType == Node.ELEMENT_NODE)
+ return children[i];
+ }
+ }
+ return null;
+ },
+
+ _render: function() {
+ if (!this.ctor)
+ this.templatize(this);
+ var parentNode = this.parentNode;
+ if (parentNode && !this._instance) {
+ this._instance = /** @type {TemplateInstance} */(this.stamp({}));
+ var root = this._instance.root;
+ parentNode.insertBefore(root, this);
+ }
+ },
+
+ /**
+ * @param {string} prop
+ * @param {Object} value
+ */
+ _forwardParentProp: function(prop, value) {
+ if (this._instance)
+ this._instance.__setProperty(prop, value, true);
+ },
+
+ /**
+ * @param {string} path
+ * @param {Object} value
+ */
+ _forwardParentPath: function(path, value) {
+ if (this._instance)
+ this._instance._notifyPath(path, value, true);
+ }
+});
« no previous file with comments | « chrome/browser/resources/md_history/lazy_render.html ('k') | chrome/browser/resources/md_history/list_container.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698