Skip to content

Instantly share code, notes, and snippets.

@threepointone
Created July 21, 2014 06:00
Show Gist options
  • Save threepointone/9484ff2faf89411f00df to your computer and use it in GitHub Desktop.
Save threepointone/9484ff2faf89411f00df to your computer and use it in GitHub Desktop.
simple stores for flux
var util = require('util'),
_ = require('underscore'),
EventEmitter = require('events').EventEmitter
function Store(ctx, initial) {
this.state = initial || {};
EventEmitter.call(this);
this.initialize.apply(this, arguments);
}
util.inherits(Store, EventEmitter);
_.extend(Store.prototype, {
getState: function() {
return this.state;
},
setState: function(state, clear) {
if (clear) {
this.state = state;
} else {
_.extend(this.state, state);
}
this.emit('update');
},
toJSON: function() {
return this.state;
},
_noop_: function(payload, callback) {
callback();
// useful for stubbing
},
setDispatcher: function(dispatcher) {
this.$ = dispatcher
}
});
Store.extend = function(methods, statics) {
var klass = function(ctx, initial) {
Store.apply(this, arguments);
};
util.inherits(klass, Store);
_.extend(klass.prototype, methods);
_.extend(klass, statics);
return klass;
}
module.exports = Store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment