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

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: calamity@ review 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 created: function() {
17 /**
18 * @type {!Array<{
19 * localProperty: string,
20 * valueGetter: function(!BookmarksPageState)
21 * }>}
22 */
23 this.watches_ = [];
24 },
25
26 attached: function() {
27 bookmarks.Store.getInstance().addObserver(this);
28 },
29
30 detached: function() {
31 bookmarks.Store.getInstance().removeObserver(this);
32 },
33
34 /**
35 * Watches a particular part of the state tree, updating |localProperty|
36 * to the return value of |valueGetter| whenever the state changes. Eg, to
37 * keep |this.item| updated with the value of a node:
38 * watch('item', (state) => state.nodes[this.itemId]);
39 *
40 * Note that object identity is used to determine if the value has changed
41 * before updating the UI, rather than Polymer-style deep equality.
42 *
43 * @param {string} localProperty
44 * @param {function(!BookmarksPageState)} valueGetter
45 */
46 watch: function(localProperty, valueGetter) {
47 // TODO(tsergeant): Warn if localProperty is not a defined property.
48 this.watches_.push({
49 localProperty: localProperty,
50 valueGetter: valueGetter,
51 });
52 },
53
54 /**
55 * Helper to dispatch an action to the store, which will update the store
56 * data and then (possibly) flow through to the UI.
57 * @param {Action} action
58 */
59 dispatch: function(action) {
60 bookmarks.Store.getInstance().handleAction(action);
61 },
62
63 /** @param {string} newState */
64 onStateChanged: function(newState) {
65 this.watches_.forEach(function(watch) {
66 var oldValue = this[watch.localProperty];
67 var newValue = watch.valueGetter(newState);
68
69 // Avoid poking Polymer unless something has actually changed. Reducers
70 // must return new objects rather than mutating existing objects, so
71 // any real changes will pass through correctly.
72 if (oldValue == newValue)
73 return;
74
75 this[watch.localProperty] = newValue;
76 }.bind(this));
77 },
78
79 updateFromStore: function() {
80 if (bookmarks.Store.getInstance().isInitialized())
81 this.onStateChanged(bookmarks.Store.getInstance().data);
82 },
83
84 /** @return {!BookmarksPageState} */
85 getState: function() {
86 return bookmarks.Store.getInstance().data;
87 },
88 };
89
90 return {StoreClient: StoreClient};
91 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/store_client.html ('k') | chrome/browser/resources/md_bookmarks/types.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698