angularjs - UI Router Resolves - Redirect in child resolve without re-running parent resolves -
i have scoured web days. understand if redirect ($state.go, etc) in resolve, transition cancelled , resolves run again.
but, if need prevent going child state. , fetch data in resolves don't need, nor want, fetch again.
i can't believe unusual scenario people. if has idea, please me plnkr working!
http://plnkr.co/edit/gfdcrqopxkbd1zzwylo9?p=preview
var app = angular.module('plunker', ['ui.router']); app.config(['$stateprovider', function($stateprovider){ $stateprovider .state('posts', { url: "/posts", templateurl: "posts-index.html", controller: "postsindexcontroller", resolve: { posts: function(post) { console.log('hi'); return post.all() } }, onenter: function($state, posts) { if (posts.length > 1 && confirm("redirect first post?")) { console.log('howdy') // $state.go('posts.show', { postid: 3 }); } } }) .state('posts.empty', { templateurl: "posts-show.html", resolve: { saysomething: function($state) { console.log('yo') } } }) .state('posts.show', { url: "/:postid", templateurl: "posts-show.html", controller: "postsshowcontroller", resolve: { post: function($stateparams, $q, post) { console.log($stateparams); return post.find($stateparams.postid) } }, onenter: function($stateparams, $state) { if ($stateparams.postid === '1') { console.log('now') return $state.go("posts.empty") } } }) }]); app.controller("postsindexcontroller", function($scope, posts){ $scope.posts = posts }) app.controller("postsshowcontroller", function($scope, post){ $scope.post = post }) app.run(function($rootscope, $state) { $rootscope.$on('$statechangeerror', function(evt, to, toparams, from, fromparams, error) { console.log(to, toparams) console.log(from, fromparams) if (error.redirectto) { $state.transitionto(error.redirectto, error.redirecttoparams); } else { // $state.go('error', {status: error.status}) } }) }) app.factory('post', function(){ return { all: function() { return [ { id: 1, title: "post 1" }, { id: 2, title: "post 2" }, { id: 3, title: "post 3" } ] }, find: function(id) { return _(this.all()).findwhere({id: +id}) } } })
i wrote comment, went bathroom , thought more.
since there's no way resolve, suggestion (and solution i'm using after trying it) controller, instead of rejecting resolve , trying handle $statechangeerror
, resolve "redirect" child state , perform actual redirection child controller.
yes, it's ugly works.
Comments
Post a Comment