Skip to content

Instantly share code, notes, and snippets.

@jaysoo
Created July 15, 2016 17:25
Show Gist options
  • Save jaysoo/a5294151cf0f52c082683bee7ec2368b to your computer and use it in GitHub Desktop.
Save jaysoo/a5294151cf0f52c082683bee7ec2368b to your computer and use it in GitHub Desktop.
class TodoStore extends BaseStore {
// Constructor is executed in the config phase.
constructor(stores, initialState) {
super(stores, initialState) // ensures initial state loads for SSR
// Other store initialization...
}
// This is executed in the run phase, after all stores have initialized.
storeDidInitialize(stores) {
// Do some work after all stores are ready
}
// Observables
@observable todos = []
@computed get hasTodos() {
return this.todos.length > 0
}
// Actions (no side-effects outside of store)
@action loadTodos(todos) {
this.todos = todos
}
@action add(todo) {
this.todos.push(todo)
}
@action delete(todo) {
this.todos.delete(todo)
}
// Effects (potentially calls actions)
fetchTodos = async() => {
const todos = await api.getTodos()
this.loadTodos(todos)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment