Last active
November 20, 2018 12:23
-
-
Save SahidMiller/be5b22b90ffbb30e26d469fe3c3fd9a1 to your computer and use it in GitHub Desktop.
Quick AngularJS ng-redux integration with UI-Router
This file contains 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
Quick integration for ng-redux and angularjs 1.x | |
Motivation: | |
For those who... | |
1. Don't want to connect to redux directly from a controller. | |
2. Don't want to call redux connect multiple times for each resolve. | |
3. Don't want to merge multiple resolves to reuse one redux connection. | |
Explaination: | |
States.js > Definition for mapStateToResolve. Similar to UI-router resolve but can take in the state and return a mapping to resolves. | |
Config.js > Hooks into stateProvider for any states that contain mapStateToResolve and adds a UI-Router resolve* for each property. | |
*The resolve only has one dependency ReduxHelper.js where we can fetch the result of connect when it's ready and changes. | |
Run.js > Hooks into stateProvider stateChangedStart event to call ReduxHelper.js, nows the time to connect to the store for this state. | |
ReduxHelper.js > | |
connect: connects to Redux store using the provided mapStateToResolve and stores the results internally using references. | |
get: fetches a reference to the results which is updated via $timeout. | |
Todo: | |
Configure default values/references using existing resolves that match mapStateToResolve | |
Integrate/configure disconnect into UI-router view and resolve lifecycle | |
Integrate/configure redux-ui-router or something similar |
This file contains 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
app.config(function($stateProvider) { | |
$stateProvider.decorator('views', function(state, parent) { | |
return state.mapStateToResolve ? mapStateToResolve(parent(state)) : parent(state); | |
}); | |
//Update resolves with mapStateToResolve resolvers, will return from reduxHelper | |
//TODO: If resolve already exists, use it as initial state? | |
function mapStateToResolve(views) { | |
var result = {}; | |
angular.forEach(views, function(config, name) { | |
if (!(config && config.$context && config.$context.mapStateToResolve)) { | |
result[name] = config; | |
return; | |
} | |
var mapStateToResolve = _.reduce(_.keys(config.$context.mapStateToResolve), function(mapStateToResolve, resolveKey) { | |
//Add resolve (or replace) with reduxHelper to get current reference to state. | |
//TODO: Use existing resolves as initial state? | |
mapStateToResolve[resolveKey] = function(reduxHelper) { | |
return reduxHelper.get(resolveKey); | |
}; | |
return mapStateToResolve; | |
}, {}); | |
config.$context.resolve = Object.assign(config.$context.resolve, mapStateToResolve); | |
result[name] = config; | |
}) | |
return result; | |
} | |
} |
This file contains 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
app.factory('reduxHelper', function($ngRedux, $injector, $timeout) { | |
var currentState = {}; | |
return { | |
get: function(key) { | |
return currentState[key]; | |
}, | |
connect: connect | |
}; | |
function connect(mapStateToResolve) { | |
return $ngRedux.connect(function(state) { | |
var mappedState = {}; | |
_.mapObject(mapStateToResolve, function(resolveFn, resolveKey) { | |
mappedState[resolveKey] = $injector.invoke(resolveFn, null, Object.assign({ state: state }, mappedState)); | |
}); | |
return mappedState; | |
})(mappedState => { | |
$timeout(() => updateCurrentStateRefs(mappedState)); | |
}); | |
} | |
function updateCurrentStateRefs(newState) { | |
_.map(newState, (value, key) => { | |
currentState[key] = currentState[key] ? Object.assign(currentState[key], value) : value | |
}) | |
} | |
}); |
This file contains 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
app.run(function(reduxHelper){ | |
$rootScope.$on("$stateChangeStart", function(event, next, nextParams) { | |
if(next && next.mapStateToResolve) { | |
var disconnect = reduxHelper.connect(next.mapStateToResolve); | |
} | |
}); | |
}); |
This file contains 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
$stateProvider.state('app', { | |
url:'/', | |
templateUrl : 'index.html', | |
controller: 'appController', | |
params: { | |
id: { | |
value: 0, | |
squash: true | |
} | |
}, | |
mapStateToResolve: { | |
items: function($stateParams, state) { | |
var id = $stateParams.id || 0; | |
return state.items && state.items[id]; | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dat lodashhh
go all the way and swap out Object.assign