Skip to content

Commit daca57f

Browse files
committed
Updated TodoMVC example to use the new FluxStore
1 parent 37fe246 commit daca57f

File tree

4 files changed

+45
-58
lines changed

4 files changed

+45
-58
lines changed

examples/flux-todomvc/js/components/TodoApp.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ var TodoApp = React.createClass({
3535
},
3636

3737
componentDidMount: function() {
38-
TodoStore.addChangeListener(this._onChange);
38+
TodoStore.addListener(this._onChange);
3939
},
4040

4141
componentWillUnmount: function() {
42-
TodoStore.removeChangeListener(this._onChange);
42+
TodoStore.removeListener(this._onChange);
4343
},
4444

4545
/**

examples/flux-todomvc/js/stores/TodoStore.js

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
*/
1111

1212
var AppDispatcher = require('../dispatcher/AppDispatcher');
13-
var EventEmitter = require('events').EventEmitter;
13+
var FluxStore = require('flux/lib/FluxStore');
1414
var TodoConstants = require('../constants/TodoConstants');
1515
var assign = require('object-assign');
1616

17-
var CHANGE_EVENT = 'change';
18-
1917
var _todos = {};
2018

2119
/**
@@ -74,101 +72,85 @@ function destroyCompleted() {
7472
}
7573
}
7674

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;
7882

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;
8891
}
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);
11692
}
117-
});
93+
return true;
94+
};
11895

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+
};
122103

104+
TodoStore.prototype.__onDispatch = function(action) {
123105
switch(action.actionType) {
124106
case TodoConstants.TODO_CREATE:
125107
text = action.text.trim();
126108
if (text !== '') {
127109
create(text);
128-
TodoStore.emitChange();
110+
this.__emitChange();
129111
}
130112
break;
131113

132114
case TodoConstants.TODO_TOGGLE_COMPLETE_ALL:
133-
if (TodoStore.areAllComplete()) {
115+
if (this.areAllComplete()) {
134116
updateAll({complete: false});
135117
} else {
136118
updateAll({complete: true});
137119
}
138-
TodoStore.emitChange();
120+
this.__emitChange();
139121
break;
140122

141123
case TodoConstants.TODO_UNDO_COMPLETE:
142124
update(action.id, {complete: false});
143-
TodoStore.emitChange();
125+
this.__emitChange();
144126
break;
145127

146128
case TodoConstants.TODO_COMPLETE:
147129
update(action.id, {complete: true});
148-
TodoStore.emitChange();
130+
this.__emitChange();
149131
break;
150132

151133
case TodoConstants.TODO_UPDATE_TEXT:
152134
text = action.text.trim();
153135
if (text !== '') {
154136
update(action.id, {text: text});
155-
TodoStore.emitChange();
137+
this.__emitChange();
156138
}
157139
break;
158140

159141
case TodoConstants.TODO_DESTROY:
160142
destroy(action.id);
161-
TodoStore.emitChange();
143+
this.__emitChange();
162144
break;
163145

164146
case TodoConstants.TODO_DESTROY_COMPLETED:
165147
destroyCompleted();
166-
TodoStore.emitChange();
148+
this.__emitChange();
167149
break;
168150

169151
default:
170152
// no op
171153
}
172-
});
154+
};
173155

174-
module.exports = TodoStore;
156+
module.exports = new TodoStore(AppDispatcher);

examples/flux-todomvc/js/stores/__tests__/TodoStore-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111

1212
jest.dontMock('../../constants/TodoConstants');
13+
jest.dontMock('../../dispatcher/AppDispatcher');
14+
jest.dontMock('flux/lib/Dispatcher');
15+
jest.dontMock('flux/lib/FluxStore');
1316
jest.dontMock('../TodoStore');
1417
jest.dontMock('object-assign');
1518

@@ -32,6 +35,8 @@ describe('TodoStore', function() {
3235

3336
beforeEach(function() {
3437
AppDispatcher = require('../../dispatcher/AppDispatcher');
38+
AppDispatcher.register = jest.genMockFn();
39+
AppDispatcher._isDispatching = jest.genMockFn().mockReturnValue(true);
3540
TodoStore = require('../TodoStore');
3641
callback = AppDispatcher.register.mock.calls[0][0];
3742
});
@@ -72,7 +77,7 @@ describe('TodoStore', function() {
7277
expect(TodoStore.areAllComplete()).toBe(false);
7378

7479
var all = TodoStore.getAll();
75-
for (key in all) {
80+
for (var key in all) {
7681
callback({
7782
actionType: TodoConstants.TODO_COMPLETE,
7883
id: key

examples/flux-todomvc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "js/app.js",
77
"dependencies": {
88
"classnames": "^2.1.3",
9-
"flux": "^2.0.1",
9+
"flux": "^2.1.0",
1010
"keymirror": "~0.1.0",
1111
"object-assign": "^1.0.0",
1212
"react": "^0.12.0"

0 commit comments

Comments
 (0)