Skip to content

Instantly share code, notes, and snippets.

@apzentral
Last active August 29, 2015 14:15
Show Gist options
  • Save apzentral/a36422ca8fd657fefdf1 to your computer and use it in GitHub Desktop.
Save apzentral/a36422ca8fd657fefdf1 to your computer and use it in GitHub Desktop.
ReactJS: Skeleton for Store
/**
* Your Store
*/
'use strict';
/**
* Libraries
*/
var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var Constants = require('../constants/Constants');
/**
* Variables
*/
var CHANGE_EVENT = 'change';
var DEBUG = false;
var _name = 'YourStore';
/**
* Store Start
*/
var YourStore = assign({}, EventEmitter.prototype, {
// Emit Change event
emitChange: function() {
this.emit(CHANGE_EVENT);
},
// Add change listener
addChangeListener: function(callback) {
this.on('change', callback);
},
// Remove change listener
removeChangeListener: function(callback) {
this.removeListener('change', callback);
}
});
/**
* Integrated with Dispatcher
*/
AppDispatcher.register(function(payload) {
var action = payload.action;
if (DEBUG) {
console.log('[*] ' + _name + ':Dispatch-Begin --- ' + action.actionType);
console.log(' Payload:');
console.log(payload);
}
// Route Logic
switch (action.actionType) {
case Constants.ACTION:
break;
default:
if (DEBUG) {
console.log('[x] ' + _name + ':actionType --- NOT MATCH');
}
return true;
}
// If action was responded to, emit change event
YourStore.emitChange();
if (DEBUG) {
console.log('[*] ' + _name + ':emitChange ---');
}
return true;
});
module.exports = YourStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment