Skip to content

Instantly share code, notes, and snippets.

@cobbweb
Created February 17, 2015 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobbweb/e5feba79193e989d6a36 to your computer and use it in GitHub Desktop.
Save cobbweb/e5feba79193e989d6a36 to your computer and use it in GitHub Desktop.
Reactive data layers with Flux
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