Created
July 24, 2015 00:27
-
-
Save nigelwtf/20f20b35d8d8d51b69d2 to your computer and use it in GitHub Desktop.
DeloreanJS Flux Dispatcher Actions
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
| # Author: https://github.com/darcyadams | |
| # Context: https://github.com/deloreanjs/delorean/issues/89#issuecomment-124233890 | |
| # Separating dispatcher concerns in DeloreanJS | |
| {Flux} = require 'delorean' | |
| Global = require './stores/global' | |
| System = require './stores/system' | |
| UserStore = require './stores/loggedin_user' | |
| Patients = require './stores/patients' | |
| PopoverModal = require './stores/popover_modal' | |
| Chart = require './stores/chart' | |
| # Create a baseline object to feed to createDispatcher | |
| dispatcher = | |
| viewTriggers: {} | |
| getStores: -> | |
| { | |
| global: Global | |
| system: System | |
| loggedInUser: UserStore | |
| patients: Patients | |
| popoverModal: PopoverModal | |
| chart: Chart | |
| } | |
| # This function takes and action file, and adds it's methods (actions) and viewTriggers to the baseline object above | |
| # it console.warns when a name conflict is detected | |
| processActions = (actionModule) -> | |
| for actionName, action of actionModule | |
| if actionName is 'viewTriggers' | |
| for triggerName, triggerMethod of action | |
| if dispatcher.viewTriggers[triggerName]? then console?.warn "Duplicate View Trigger: #{triggerName}. #{triggerName} will not be applied to the dispatcher viewTriggers hash." | |
| else dispatcher.viewTriggers[triggerName] = triggerMethod | |
| else | |
| if dispatcher[actionName]? then console?.warn "Duplicate Dispatcher Action: #{actionName}. #{actionName} will not be applied to the dispatcher." | |
| else dispatcher[actionName] = action | |
| # Require action modules inside of this Array | |
| # This goes through the separate action files and runs them through the processActions function above, which adds them to the dispatcher | |
| processActions(actionModule) for actionModule in [ | |
| require './actions/system' | |
| require './actions/loggedin_user' | |
| require './actions/patients' | |
| require './actions/popover_modal' | |
| require './actions/chart' | |
| require './actions/global' | |
| ] | |
| module.exports = Flux.createDispatcher dispatcher |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment