Created
June 23, 2017 14:41
-
-
Save whyu/4b9802e77e817e371bbf21051de03c50 to your computer and use it in GitHub Desktop.
Wrap flux dispatcher
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
| function wrapFlux(MainApp, availableActions, defaultStore) { | |
| return React.createClass({ | |
| getInitialState: function() { | |
| return $.extend({}, defaultStore, this.props.additionalData); | |
| }, | |
| dispatcher: function(action, extras) { | |
| // if an array of actions is passed in | |
| if (Array.isArray(action)) { | |
| var promiseArr = []; // Create Promise Array | |
| for (var i=0; i<action.length; i++) { // Loop through Actions | |
| var actionToDo = availableActions[action[i]]; // Find actionToDo from List | |
| var promise = actionToDo(this.state, extras); // create promise from actionToDo | |
| console.log(actionToDo, promise, "ActionToDo and Promise"); | |
| if (!promise) return; | |
| promiseArr.push(promise); | |
| } | |
| // console.log(promiseArr, 'promiseArr'); | |
| promiseArr.reduce(function(prev, curr){ | |
| // console.log(prev, curr, 'prev,curr'); | |
| return prev.then(curr); | |
| }, Q()).then(function(data){ | |
| // console.log('test', data, this); | |
| var newState = actionToDo(this.state, extras).then(function(newState){ | |
| this.setState(newState); | |
| console.log('setting State', newState); | |
| }.bind(this)); | |
| return data; | |
| }.bind(this)); | |
| return; | |
| } | |
| var actionToDo = availableActions[action]; | |
| if (!actionToDo) { | |
| return; | |
| } | |
| var newState = actionToDo(this.state, extras).then(function(newState){ | |
| this.setState(newState); | |
| }.bind(this)); | |
| }, | |
| render: function() { | |
| return <MainApp store={this.state} dispatcher={this.dispatcher} /> | |
| }, | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment