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

Unified Diff: chrome/browser/resources/md_bookmarks/store_client.js

Issue 2731473002: MD Bookmarks: Implement basis for new data-binding system (Closed)
Patch Set: Add some !s I missed Created 3 years, 10 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_bookmarks/store_client.js
diff --git a/chrome/browser/resources/md_bookmarks/store_client.js b/chrome/browser/resources/md_bookmarks/store_client.js
new file mode 100644
index 0000000000000000000000000000000000000000..7d11047c4666b1582e31556e3c5b6ad9a38b8482
--- /dev/null
+++ b/chrome/browser/resources/md_bookmarks/store_client.js
@@ -0,0 +1,89 @@
+// Copyright 2017 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 Defines StoreClient, a Polymer behavior to tie a front-end
+ * element to back-end data from the store.
+ */
+
+cr.define('bookmarks', function() {
+ /**
+ * @polymerBehavior
+ * @implements {StoreObserver}
+ */
+ var StoreClient = {
+ attached: function() {
+ /**
+ * @type {!Array<{
+ * localProperty: string,
+ * valueGetter: function(!BookmarksPageState)
+ * }>}
+ */
+ this.observers_ = [];
+ bookmarks.Store.getInstance().addObserver(this);
+ },
+
+ detached: function() {
+ bookmarks.Store.getInstance().removeObserver(this);
+ },
+
+ /**
+ * Observes a particular part of the state tree, updating |localProperty|
+ * to the return value of |valueGetter| whenever the state changes. Eg, to
+ * keep |this.item| updated with the value of a node:
+ * observe('item', (state) => state.nodes[this.itemId]);
+ *
+ * Note that object identity is used to determine if the value has changed
+ * before updating the UI, rather than Polymer-style deep equality.
+ *
+ * @param {string} localProperty
+ * @param {function(!BookmarksPageState)} valueGetter
+ */
+ observe: function(localProperty, valueGetter) {
calamity 2017/03/03 02:29:03 Is it possible to check here that localProperty is
tsergeant 2017/03/03 05:08:00 Hmmm. Maybe? Checking if it's a Polymer property o
+ this.observers_.push({
+ localProperty: localProperty,
+ valueGetter: valueGetter,
+ });
+ },
+
+ /**
+ * Helper to dispatch an action to the store, which will update the store
+ * data and then (possibly) flow through to the UI.
+ * @param {Action} action
+ */
+ dispatch: function(action) {
+ bookmarks.Store.getInstance().handleAction(action);
+ },
+
+ /** @param {string} newState */
+ onStateChanged: function(newState) {
+ if (!this.observers_)
+ return;
calamity 2017/03/03 02:29:03 nit: newline. When can this happen?
tsergeant 2017/03/03 05:08:00 While initializing <bookmarks-item> or <bookmarks-
calamity 2017/03/03 05:46:09 Wew.
+ this.observers_.forEach(function(observer) {
+ var oldValue = this[observer.localProperty];
+ var newValue = observer.valueGetter(newState);
+
+ // Avoid poking Polymer unless something has actually changed. Reducers
+ // must return new objects rather than mutating existing objects, so
+ // any real changes will pass through correctly.
+ if (oldValue == newValue)
+ return;
+
+ this[observer.localProperty] = newValue;
+ }.bind(this));
+ },
+
+ updateFromStore: function() {
+ if (bookmarks.Store.getInstance().isInitialized())
+ this.onStateChanged(bookmarks.Store.getInstance().data);
+ },
+
+ /** @return {!BookmarksPageState} */
+ getState: function() {
+ return bookmarks.Store.getInstance().data;
+ },
+ };
+
+ return {StoreClient: StoreClient};
+});

Powered by Google App Engine
This is Rietveld 408576698