const AppDispatcher = require('../dispatcher/AppDispatcher'); const EventEmitter = require('events').EventEmitter; const Constants = require('../constants/Constants'); const assign = require('object-assign'); let appData = {}; const AppStore = assign({}, EventEmitter.prototype, { getData: function () { return appData; }, emitChange: function () { this.emit(Constants.CHANGE_EVENT); }, addChangeListener: function (callback) { this.on(Constants.CHANGE_EVENT, callback); }, removeChangeListener: function (callback) { this.removeListener(Constants.CHANGE_EVENT, callback); } }); AppDispatcher.register(function (payload) { let action = payload.action; switch (action.actionType) { case Constants.UPDATE_DATA: appData = action.data; AppStore.emitChange(); break; // add more cases here for different actions default: return true; } }); module.exports = AppStore;
class CounterStore extends FluxStore { constructor(dispatcher) { super(dispatcher); this._counter = 0; } getCount() { return this._counter; } __onDispatch(action) { switch (action.type) { case 'INCREMENT': this._counter++; this.__emitChange(); break; case 'RESET': this._counter = 0; this.__emitChange(); break; } } }This example is a simplified version of a Store that manages a counter. The `getCount` method is used to retrieve the current value of the counter. The Store listens to actions dispatched by the Dispatcher and updates the counter accordingly. When the counter is updated, the Store uses `__emitChange` to notify the view components. Package library used: FluxStore (part of the Flux architecture)