11. Implements Action
var Actions = Reflux.createActions([
"statusUpdate",
"statusEdited",
"statusAdded"
]);
// Actions object now contains the actions
// with the names given in the array above
// that may be invoked as usual
Actions.statusUpdate();
13. Manually Listenable Store
class StatusStore extends Reflux.Store
{
constructor()
{
super();
this.state = {flag:’OFFLINE’};
// <- set store's default state much like in React
this.listenTo(statusUpdate, this.onStatusUpdate);
// listen to the statusUpdate action
}
onStatusUpdate(status)
{
var newFlag = status ? 'ONLINE' : 'OFFLINE';
this.setState({flag:newFlag});
}
}
14. var Actions = Reflux.createActions([
‘firstAction’,
‘secondAction’
]);
class StatusStore extends Reflux.Store
{
constructor()
{
super();
this.listenables = Actions;
}
onFirstAction()
{
// calls on Actions.firstAction();
}
onSecondAction()
{
// calls on Actions.secondAction();
Auto Listenable Store
16. Set simple Store
class MyComponent extends Reflux.Component
{
constructor(props)
{
super(props);
this.state = {};
// our store will add its own state to the
component's
this.store = StatusStore;
// <- just assign the store class itself
}
render()
{
var flag = this.state.flag; // <- flag is mixed in
from the StatusStore
return <div>User is {flag}</div>
}
}
17. Set multiple Stores
class MyComponent extends Reflux.Component
{
constructor(props)
{
super(props);
this.state = {type:’admin'};
// <- note that we can still use normal state
this.stores = [StatusStore, AnotherStore];
this.storeKeys = ['flag', 'info'];
}
render()
{
var flag = this.state.flag;
var info = this.state.info;
var type = this.state.type;
return <div>User is {flag}, info: {info}, type:
{type}</div>
}
}