Skip to content

Instantly share code, notes, and snippets.

@mrgenixus
Last active February 24, 2016 21:06
Show Gist options
  • Select an option

  • Save mrgenixus/2dc32abb266aaa6a4066 to your computer and use it in GitHub Desktop.

Select an option

Save mrgenixus/2dc32abb266aaa6a4066 to your computer and use it in GitHub Desktop.
Proposed base store
export default FluxStore.extend({
model: 'store',
events: {
"ASSOCIATE_STORE": 'associate_store'
},
create(payload) {
return Store.create(payload.data)
},
associate_store(payload) {
Store.ingredients.add(payload.data);
}
})
let DEFAULT_ACTIONS = /(GET|UPDATE|DESTROY|CREATE|LIST)_/
export default function FluxStore(options) {
this.dispatcher = options.dispatcher
this.dispatcher.register(this.handleEvent.bind(this))
FluxStore.prototype.handleEvent = function handleEvent(payload) {
if(this.events && this.events[payload.type]) {
let handler = this[this.events[payload.type]];
if (_.isFunction()) {
handler.call(this, payload);
}
} else if (this.matchModel(payload.type) and this.actionDefined(payload.type)) {
this.callAction(payload)
} else {
return
}
}
FluxStore.prototype.matchModel = function matchModel(type){
return this.modelRegex().test(type);
}
FluxStore.prototype.modelRegex = function modelRegex() {
return new RegExp(
'((GET|UPDATE|DESTROY|CREATE)_' + this.model.toUpperCase() + '' +
'|' +
'LIST_' + this.model.toUpperCase() + 'S)$'
);
};
FluxStore.prototype.actionDefined = function actionDefined(type) {
return DEFAULT_ACTIONS.test(type) &&
_.isFunction(this[DEFAULT_ACTIONS.exec(type)[1].toLowerCase()]);
}
FluxStore.prototype.callAction = function callAction(payload) {
this[DEFAULT_ACTIONS.exec(type)[1].toLowerCase()].call(this, payload);
}
export default FluxStore.extend({
model: 'store',
create(options) {
return Store.create(options)
}
})
@apotheon
Copy link

Does that regex line at the beginning of flux_store.js need a tilde in it, or is JavaScript just that weird?

@apotheon
Copy link

I'm willing to use this if someone documents its intended use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment