|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | var AppDispatcher = require('../dispatcher/AppDispatcher'); |
13 | | -var EventEmitter = require('events').EventEmitter; |
| 13 | +var FluxStore = require('flux/lib/FluxStore'); |
14 | 14 | var TodoConstants = require('../constants/TodoConstants'); |
15 | 15 | var assign = require('object-assign'); |
16 | 16 |
|
17 | | -var CHANGE_EVENT = 'change'; |
18 | | - |
19 | 17 | var _todos = {}; |
20 | 18 |
|
21 | 19 | /** |
@@ -74,101 +72,85 @@ function destroyCompleted() { |
74 | 72 | } |
75 | 73 | } |
76 | 74 |
|
77 | | -var TodoStore = assign({}, EventEmitter.prototype, { |
| 75 | +// create a TodoStore subclass by doing classical inheritance |
| 76 | +// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create |
| 77 | +function TodoStore(dispatcher) { |
| 78 | + FluxStore.call(this, dispatcher); // call super constructor. |
| 79 | +} |
| 80 | +TodoStore.prototype = Object.create(FluxStore.prototype); |
| 81 | +TodoStore.prototype.constructor = TodoStore; |
78 | 82 |
|
79 | | - /** |
80 | | - * Tests whether all the remaining TODO items are marked as completed. |
81 | | - * @return {boolean} |
82 | | - */ |
83 | | - areAllComplete: function() { |
84 | | - for (var id in _todos) { |
85 | | - if (!_todos[id].complete) { |
86 | | - return false; |
87 | | - } |
| 83 | +/** |
| 84 | + * Tests whether all the remaining TODO items are marked as completed. |
| 85 | + * @return {boolean} |
| 86 | + */ |
| 87 | +TodoStore.prototype.areAllComplete = function() { |
| 88 | + for (var id in _todos) { |
| 89 | + if (!_todos[id].complete) { |
| 90 | + return false; |
88 | 91 | } |
89 | | - return true; |
90 | | - }, |
91 | | - |
92 | | - /** |
93 | | - * Get the entire collection of TODOs. |
94 | | - * @return {object} |
95 | | - */ |
96 | | - getAll: function() { |
97 | | - return _todos; |
98 | | - }, |
99 | | - |
100 | | - emitChange: function() { |
101 | | - this.emit(CHANGE_EVENT); |
102 | | - }, |
103 | | - |
104 | | - /** |
105 | | - * @param {function} callback |
106 | | - */ |
107 | | - addChangeListener: function(callback) { |
108 | | - this.on(CHANGE_EVENT, callback); |
109 | | - }, |
110 | | - |
111 | | - /** |
112 | | - * @param {function} callback |
113 | | - */ |
114 | | - removeChangeListener: function(callback) { |
115 | | - this.removeListener(CHANGE_EVENT, callback); |
116 | 92 | } |
117 | | -}); |
| 93 | + return true; |
| 94 | +}; |
118 | 95 |
|
119 | | -// Register callback to handle all updates |
120 | | -AppDispatcher.register(function(action) { |
121 | | - var text; |
| 96 | +/** |
| 97 | + * Get the entire collection of TODOs. |
| 98 | + * @return {object} |
| 99 | + */ |
| 100 | +TodoStore.prototype.getAll = function() { |
| 101 | + return _todos; |
| 102 | +}; |
122 | 103 |
|
| 104 | +TodoStore.prototype.__onDispatch = function(action) { |
123 | 105 | switch(action.actionType) { |
124 | 106 | case TodoConstants.TODO_CREATE: |
125 | 107 | text = action.text.trim(); |
126 | 108 | if (text !== '') { |
127 | 109 | create(text); |
128 | | - TodoStore.emitChange(); |
| 110 | + this.__emitChange(); |
129 | 111 | } |
130 | 112 | break; |
131 | 113 |
|
132 | 114 | case TodoConstants.TODO_TOGGLE_COMPLETE_ALL: |
133 | | - if (TodoStore.areAllComplete()) { |
| 115 | + if (this.areAllComplete()) { |
134 | 116 | updateAll({complete: false}); |
135 | 117 | } else { |
136 | 118 | updateAll({complete: true}); |
137 | 119 | } |
138 | | - TodoStore.emitChange(); |
| 120 | + this.__emitChange(); |
139 | 121 | break; |
140 | 122 |
|
141 | 123 | case TodoConstants.TODO_UNDO_COMPLETE: |
142 | 124 | update(action.id, {complete: false}); |
143 | | - TodoStore.emitChange(); |
| 125 | + this.__emitChange(); |
144 | 126 | break; |
145 | 127 |
|
146 | 128 | case TodoConstants.TODO_COMPLETE: |
147 | 129 | update(action.id, {complete: true}); |
148 | | - TodoStore.emitChange(); |
| 130 | + this.__emitChange(); |
149 | 131 | break; |
150 | 132 |
|
151 | 133 | case TodoConstants.TODO_UPDATE_TEXT: |
152 | 134 | text = action.text.trim(); |
153 | 135 | if (text !== '') { |
154 | 136 | update(action.id, {text: text}); |
155 | | - TodoStore.emitChange(); |
| 137 | + this.__emitChange(); |
156 | 138 | } |
157 | 139 | break; |
158 | 140 |
|
159 | 141 | case TodoConstants.TODO_DESTROY: |
160 | 142 | destroy(action.id); |
161 | | - TodoStore.emitChange(); |
| 143 | + this.__emitChange(); |
162 | 144 | break; |
163 | 145 |
|
164 | 146 | case TodoConstants.TODO_DESTROY_COMPLETED: |
165 | 147 | destroyCompleted(); |
166 | | - TodoStore.emitChange(); |
| 148 | + this.__emitChange(); |
167 | 149 | break; |
168 | 150 |
|
169 | 151 | default: |
170 | 152 | // no op |
171 | 153 | } |
172 | | -}); |
| 154 | +}; |
173 | 155 |
|
174 | | -module.exports = TodoStore; |
| 156 | +module.exports = new TodoStore(AppDispatcher); |
0 commit comments