-
Notifications
You must be signed in to change notification settings - Fork 16
Actions and Stores for FLUX
FLUX pattern is the fundamental architecture Lantern used, which includes
Actions
, Stores
and Views
, the data flow in only one direction on. With Fluky, everything is asynchronous event. Actions and stores are easily created with event listening mechanism.
Fluky Lantern used is an event-based FLUX framework that implemented Actions and Stores with general event mechanism. All of actions and stores are processed by passing event dispatcher.
Create a new JavaScript file in the ./src/js/actions
, and write a generator function in it like below:
Example foo.js:
export default function *() {
};
export default function *() {
this.on('action.Foo.doSomething', function *() {
// do something...
});
};
export default function *() {
this.on('action.Foo.doSomething', function *() {
this.dispatch('store.Foo.doSomething');
});
};
Note that Fluky will forward events with a new event name store.*
to Stores automatically if no action event listener.
Write the file name above to ./src/actions/index.js
.
export default {
Foo: require('./foo.js')
};
Create a new JavaScript file in the ./src/js/stores
, and write a generator function in it like below:
Example foo.js:
export default function *() {
};
export default function *() {
this.on('store.Foo.doSomething', function *() {
// do something...
});
};
export default function *() {
var store = this.getState('Foo', {
key1: 100,
key2: 'Hello'
});
this.on('store.Foo.doSomething', function *() {
// do something...
});
};
export default function *() {
var store = this.getState('Foo', {
key1: 100,
key2: 'Hello'
});
this.on('store.Foo.doSomething', function *() {
var state = this.getState('Foo');
// Updating state
state.key1++;
// Fire event to tell all of listeners that `state.Foo` was changed
this.dispatch('state.Foo');
});
};
Write the file name above to ./src/stores/index.js
.
export default {
Foo: require('./foo.js')
};