Last active
August 29, 2015 14:17
-
-
Save goatslacker/343c7d17e350e131420e to your computer and use it in GitHub Desktop.
This file contains 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
var AppDispatcher = require('./dispatcher.js'); | |
var TodoActions = { | |
create(text) { | |
AppDispatcher.dispatch({ | |
actionType: 'CREATE_TODO' | |
text: text | |
}); | |
} | |
}; |
This file contains 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
var Dispatcher = require('flux').Dispatcher; | |
module.exports = new Dispatcher(); |
This file contains 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
var AppDispatcher = require('./dispatcher.js'); | |
var EventEmitter = require('events').EventEmitter; | |
var _todos = {}; | |
function create(text) { | |
var id = String(Date.now()) + String(Math.random()); | |
_todos[id] = { | |
id: id, | |
text: text | |
}; | |
} | |
var TodoStore = Object.assign({}, EventEmitter.prototype, { | |
getTodos() { | |
return _todos; | |
}, | |
emitChange() { | |
this.emit('change'); | |
} | |
}); | |
AppDispatcher.register((action) => { | |
switch(action.actionType) { | |
case 'CREATE_TODO': | |
text = action.text.trim(); | |
if (text !== '') { | |
create(text); | |
TodoStore.emitChange(); | |
} | |
break; | |
} | |
}); |
This file contains 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
var TodoStore = require('./store.js'); | |
var TodoView = React.createClass({ | |
getInitialState() { | |
return { todos: TodoStore.getTodos() }; | |
}, | |
componentDidMount() { | |
TodoStore.addEventListener('change', this._onChange); | |
}, | |
_onChange() { | |
this.setState({ todos: TodoStore.getTodos() }); | |
}, | |
render() { | |
return ( | |
<ul> | |
{this.todos.map((todo) => { | |
return <li>{todo.text}</li> | |
})} | |
</ul> | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment