DeLoreanFlux 模式實現(xiàn)
DeLoreanjs(Delorean.js) 是極小的 Flux 模式實現(xiàn):
單向數(shù)據(jù)流,是的你的應(yīng)用邏輯比 MVC 還簡單
-
自動監(jiān)聽數(shù)據(jù)變化情況,保持數(shù)據(jù)持續(xù)更新
這是個很完整的框架,沒有視圖框架
非常小,壓縮后只有 4K
內(nèi)置 React.js 集成,易于使用 Flight.js 和 Reactive.js 或者其他的 JS 庫
使用回滾提高 UI 和數(shù)據(jù)的一致性
示例:
/* * Stores are simple data buckets which manages data. */var Store = Flux.createStore({
data: null,
setData: function (data) {
this.data = data;
this.emit('change');
},
actions: {
'incoming-data': 'setData'
}});var store = new Store();/* * Dispatchers are simple action dispatchers for stores. * Stores handle the related action. */var Dispatcher = Flux.createDispatcher({
setData: function (data) {
this.dispatch('incoming-data', data);
},
getStores: function () {
return {increment: store};
}});/* * Action Creators are simple controllers. They are simple functions. * They talk to dispatchers. They are not required. */var Actions = {
setData: function (data) {
Dispatcher.setData(data);
}};// The data cycle.store.onChange(function () {
// End of data cycle.
document.getElementById('result').innerText = store.store.data;});document.getElementById('dataChanger').onclick = function () {
// Start data cycle:
Actions.setData(Math.random());};
評論
圖片
表情
