Created
December 9, 2014 00:47
-
-
Save flarnie/7c85e99c5b344d9677fd to your computer and use it in GitHub Desktop.
Flux TodoApp Sample 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The TodoStore has registered a callback for the 'TODO_CREATE' action. | |
// ... | |
/** | |
* Create a TODO item. | |
* @param {string} text The content of the TODO | |
*/ | |
function create(text) { | |
// Hand waving here -- not showing how this interacts with XHR or persistent | |
// server-side storage. | |
// Using the current timestamp + random number in place of a real id. | |
var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); | |
_todos[id] = { | |
id: id, | |
complete: false, | |
text: text | |
}; | |
} | |
// Register to handle all updates | |
AppDispatcher.register(function(payload) { | |
var action = payload.action; | |
var text; | |
switch(action.actionType) { | |
case TodoConstants.TODO_CREATE: | |
text = action.text.trim(); | |
if (text !== '') { | |
create(text); | |
} | |
break; | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment