Created
February 17, 2015 06:22
-
-
Save cobbweb/e5feba79193e989d6a36 to your computer and use it in GitHub Desktop.
Reactive data layers with Flux
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
var alt = require('../alt'); | |
var TodoActions = require('./TodoActions'); | |
// Reactive data layer | |
var Todos = alt.Todos; | |
class TodoStore { | |
constructor() { | |
this.bindActions(TodoActions); | |
this.todos = Todos.find({}); | |
// Listen for data pushed via WebSocket | |
Todos.on('change', this.refresh.bind(this)); | |
} | |
onCreate(text) { | |
if (!this.getInstance()) { | |
// prevent pre-init calls | |
return true; | |
} | |
Todos.insert({ text: text }); | |
} | |
onRemove(_id) { | |
if (!this.getInstance()) { | |
// prevent pre-init calls | |
return true; | |
} | |
Todos.remove(_id); | |
} | |
refresh() { | |
if (!this.getInstance()) { | |
// prevent pre-init calls | |
return true; | |
} | |
this.todos = Todos.find({}); | |
this.getInstance().emitChange(); | |
} | |
} | |
module.exports = alt.createStore(TodoStore); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment