r3plica
r3plica

Reputation: 13397

$state.go in resolve not working

So I have these routes set up:

.state('createOrder', {
    url: '/customer-services/orders/create',
    templateUrl: 'tpl/customerServices/orders/save.html',
    controller: 'SaveOrderController',
    controllerAs: 'controller',
    resolve: {
        order: ['SaveOrderService', function (shared) {
            shared.order = { forDelivery: true };
        }]
    },
    data: {
        requireLogin: true,
        pageTitle: 'Add order'
    }
}).state('createOrder.lines', {
    url: '/lines',
    views: {
        '@': {
            templateUrl: 'tpl/customerServices/orders/save/line.html',
            controller: 'SaveOrderLinesController',
            controllerAs: 'controller'
        }
    },
    resolve: {
        validate: ['$state', 'SaveOrderService', function ($state, shared) {

            // If we don't have an account number
            if (!shared.order.accountNumber) {

                console.log('redirecting');

                // Redirect to the create order view
                $state.go('createOrder');
            }
        }]
    },
    data: {
        requireLogin: true,
        pageTitle: 'Add order : Lines'
    }
})

But the state does not change. I thought that there might be an error somewhere, so I subscribed the the state events like this:

// On state change
$rootScope.$on('$stateChangeStart', function (event, toState) {
    var data = toState.data; // Get our state data
    var requireLogin = typeof data === 'undefined' ? false : data.requireLogin; // Check to see if we have any data and if so, check to see if we need login rights
    var user = service.get(); // Get our current user

    console.log(toState);

    $rootScope.currentUser = user; // Set our current user on the rootScope

    // If we require login rights and we are not authenticated
    if (requireLogin && !user.authenticated) {
        event.preventDefault(); // Stop processing

        $state.transitionTo('login'); // And redirect to the login page
    }
});

$rootScope.$on('$stateNotFound', function () {
    console.log('state not found');
});

$rootScope.$on('$stateChangeError', function () {
    console.log('state errored');
});

$rootScope.$on('$stateChangeSuccess', function () {
    console.log('state changed');
});

and when I refresh my lines view, the console outputs this:

Object { url: "/lines", views: Object, resolve: Object, data: Object, name: "createOrder.lines" } app.js:42:9
redirecting app.js:2436:21
Object { url: "/customer-services", templateUrl: "tpl/customerServices/index.html", controller: "CustomerServicesController", controllerAs: "controller", data: Object, name: "customerServices" } app.js:42:9
state changed app.js:63:9
Object { order: Object, account: null, collectionPoint: null }

As you can see, the states think they have changed, but I still see the createOrder.lines view. Does anyone have any idea why?

Upvotes: 1

Views: 409

Answers (2)

r3plica
r3plica

Reputation: 13397

So, it turns out you don't need the promise. Just adding the timeout works. I found another post which suggests that the timeout is needed to avoid digest issues (which I am guessing is what is causing my states to not change). Here is the final code:

validate: ['$state', '$timeout', 'SaveOrderService', function ($state, $timeout, shared) {

    // If we don't have an account number
    if (!shared.order.accountNumber) {

        // Timeout to avoid digest issues
        $timeout(function () {

            // Redirect to the create order view
            $state.go('createOrder');
        });
    }
}]

Upvotes: 0

CainBot
CainBot

Reputation: 434

I think you'll need to wrap the $state change in a function that will trigger a digest cycle whilst also rejecting the promise in the resolve method...

$timeout(function() { $state.go("createOrder") });
return $q.reject("Rejection message!"); 

Remember to inject $timeout and $q into your resolve function! =)

Should also add that rejecting the resolve will fire stateChangeError.

Upvotes: 1

Related Questions