Last active
February 24, 2016 21:06
-
-
Save mrgenixus/2dc32abb266aaa6a4066 to your computer and use it in GitHub Desktop.
Proposed base store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default FluxStore.extend({ | |
| model: 'store', | |
| create(options) { | |
| return Store.create(options) | |
| } | |
| }) |
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
Does that regex line at the beginning of
flux_store.jsneed a tilde in it, or is JavaScript just that weird?