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

Side by Side 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, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 Defines StoreClient, a Polymer behavior to tie a front-end
7 * element to back-end data from the store.
8 */
9
10 cr.define('bookmarks', function() {
11 /**
12 * @polymerBehavior
13 * @implements {StoreObserver}
14 */
15 var StoreClient = {
16 attached: function() {
17 /**
18 * @type {!Array<{
19 * localProperty: string,
20 * valueGetter: function(!BookmarksPageState)
21 * }>}
22 */
23 this.observers_ = [];
24 bookmarks.Store.getInstance().addObserver(this);
25 },
26
27 detached: function() {
28 bookmarks.Store.getInstance().removeObserver(this);
29 },
30
31 /**
32 * Observes a particular part of the state tree, updating |localProperty|
33 * to the return value of |valueGetter| whenever the state changes. Eg, to
34 * keep |this.item| updated with the value of a node:
35 * observe('item', (state) => state.nodes[this.itemId]);
36 *
37 * Note that object identity is used to determine if the value has changed
38 * before updating the UI, rather than Polymer-style deep equality.
39 *
40 * @param {string} localProperty
41 * @param {function(!BookmarksPageState)} valueGetter
42 */
43 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
44 this.observers_.push({
45 localProperty: localProperty,
46 valueGetter: valueGetter,
47 });
48 },
49
50 /**
51 * Helper to dispatch an action to the store, which will update the store
52 * data and then (possibly) flow through to the UI.
53 * @param {Action} action
54 */
55 dispatch: function(action) {
56 bookmarks.Store.getInstance().handleAction(action);
57 },
58
59 /** @param {string} newState */
60 onStateChanged: function(newState) {
61 if (!this.observers_)
62 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.
63 this.observers_.forEach(function(observer) {
64 var oldValue = this[observer.localProperty];
65 var newValue = observer.valueGetter(newState);
66
67 // Avoid poking Polymer unless something has actually changed. Reducers
68 // must return new objects rather than mutating existing objects, so
69 // any real changes will pass through correctly.
70 if (oldValue == newValue)
71 return;
72
73 this[observer.localProperty] = newValue;
74 }.bind(this));
75 },
76
77 updateFromStore: function() {
78 if (bookmarks.Store.getInstance().isInitialized())
79 this.onStateChanged(bookmarks.Store.getInstance().data);
80 },
81
82 /** @return {!BookmarksPageState} */
83 getState: function() {
84 return bookmarks.Store.getInstance().data;
85 },
86 };
87
88 return {StoreClient: StoreClient};
89 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698