Permalink
Browse files

[TodoMVC Example] remove unnecessary handleViewAction() method, impro…

…ve emitChange() usage
  • Loading branch information...
1 parent c36a9ac commit ec8bba6893da01dc0f4e7a136d47acfd2cea3ac4 @fisherwebdev fisherwebdev committed Jan 6, 2015
@@ -18,7 +18,7 @@ var TodoActions = {
* @param {string} text
*/
create: function(text) {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_CREATE,
text: text
});
@@ -29,7 +29,7 @@ var TodoActions = {
* @param {string} text
*/
updateText: function(id, text) {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_UPDATE_TEXT,
id: id,
text: text
@@ -43,12 +43,12 @@ var TodoActions = {
toggleComplete: function(todo) {
var id = todo.id;
if (todo.complete) {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_UNDO_COMPLETE,
id: id
});
} else {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_COMPLETE,
id: id
});
@@ -59,7 +59,7 @@ var TodoActions = {
* Mark all ToDos as complete
*/
toggleCompleteAll: function() {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_TOGGLE_COMPLETE_ALL
});
},
@@ -68,7 +68,7 @@ var TodoActions = {
* @param {string} id
*/
destroy: function(id) {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_DESTROY,
id: id
});
@@ -78,7 +78,7 @@ var TodoActions = {
* Delete all the completed ToDos
*/
destroyCompleted: function() {
- AppDispatcher.handleViewAction({
+ AppDispatcher.dispatch({
actionType: TodoConstants.TODO_DESTROY_COMPLETED
});
}
@@ -12,22 +12,5 @@
*/
var Dispatcher = require('flux').Dispatcher;
-var assign = require('object-assign');
-var AppDispatcher = assign(new Dispatcher(), {
-
- /**
- * A bridge function between the views and the dispatcher, marking the action
- * as a view action. Another variant here could be handleServerAction.
- * @param {object} action The data coming from the view.
- */
- handleViewAction: function(action) {
- this.dispatch({
- source: 'VIEW_ACTION',
- action: action
- });
- }
-
-});
-
-module.exports = AppDispatcher;
+module.exports = new Dispatcher();
@@ -118,9 +118,8 @@ var TodoStore = assign({}, EventEmitter.prototype, {
}
});
-// Register to handle all updates
-AppDispatcher.register(function(payload) {
- var action = payload.action;
+// Register callback to handle all updates
+AppDispatcher.register(function(action) {
var text;
switch(action.actionType) {
@@ -129,6 +128,7 @@ AppDispatcher.register(function(payload) {
if (text !== '') {
create(text);
}
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_TOGGLE_COMPLETE_ALL:
@@ -137,42 +137,40 @@ AppDispatcher.register(function(payload) {
} else {
updateAll({complete: true});
}
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_UNDO_COMPLETE:
update(action.id, {complete: false});
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_COMPLETE:
update(action.id, {complete: true});
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_UPDATE_TEXT:
text = action.text.trim();
if (text !== '') {
update(action.id, {text: text});
}
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_DESTROY:
destroy(action.id);
+ TodoStore.emitChange();
break;
case TodoConstants.TODO_DESTROY_COMPLETED:
destroyCompleted();
+ TodoStore.emitChange();
break;
default:
- return true;
+ // no op
}
-
- // This often goes in each case that should trigger a UI change. This store
- // needs to trigger a UI change after every view action, so we can make the
- // code less repetitive by putting it here. We need the default case,
- // however, to make sure this only gets called after one of the cases above.
- TodoStore.emitChange();
-
- return true; // No errors. Needed by promise in Dispatcher.
});
module.exports = TodoStore;
@@ -1,11 +1,11 @@
{
"name": "todomvc-flux",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Example Flux architecture.",
"repository": "https://github.com/facebook/flux",
"main": "js/app.js",
"dependencies": {
- "flux": "^2.0.0",
+ "flux": "^2.0.1",
"keymirror": "~0.1.0",
"object-assign": "^1.0.0",
"react": "^0.12.0"

3 comments on commit ec8bba6

@ronag
ronag commented on ec8bba6 Jan 11, 2015

This is no longer consistent with the Chat example?

@danesparza

@ronag Consistent how? The chat example was always a bit more complicated AFAIK.

@fisherwebdev
Contributor

I'm planning to update the chat example in the same way -- remove the handleViewAction() wrapper and call dispatch() directly. Also a pull request to do that would be most welcome.

Please sign in to comment.