Skip to content

Instantly share code, notes, and snippets.

@Grokling
Created September 11, 2015 01:31
Show Gist options
  • Save Grokling/e103060e2dea6b0c0b80 to your computer and use it in GitHub Desktop.
Save Grokling/e103060e2dea6b0c0b80 to your computer and use it in GitHub Desktop.
Decorating ui-router resolves
app.config(function($stateProvider) {
// register a state decorator with ui-router. Decorate any property (I chose parent)
$stateProvider.decorator('parent', function (state, parentFn) { // read ui-router docs for decorator
// Get the resolves object for the state being registered
var resolves = state.self.resolve || {};
angular.forEach(resolves, function(resolve, key) { // for each resolve
// get the resolve function. Support array style [“$foo”, function($foo){ }]
var resolveFn = angular.isArray(resolve) ? resolve.slice(-1) : resolve;
// create a new resolve function which invokes the old resolve fn
// and chains a catch block which returns null
var stupidDecoratedResolve = function() {
return resolveFn.call(null, arguments).catch(function(err) { return null; });
};
// support resolvefn.$inject style annotations
if (resolveFn.$inject) {
stupidDecoratedResolve.$inject = resolveFn.$inject;
}
// put the new decorated resolve function back on the resolve object on the state
resolves[key] = angular.isArray(resolve) ? resolve.slice(0, -1).concat(stupidDecoratedResolve) : stupidDecoratedResolve;
});
// just do this, else ui-router wont work
return parentFn(state);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment