Created
July 15, 2016 17:25
-
-
Save jaysoo/a5294151cf0f52c082683bee7ec2368b to your computer and use it in GitHub Desktop.
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
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