I am using ui-router and ui-router extras
$scope.$on('$transitionStart', function (e, $transition$) {
var params = {id:1};
factory.webService(params)
.success(function (data) {
if (data.d == "Expired") {//Inspects the application session
//Stops the state request and sents you to the login screen
e.preventDefault();
location.href = "login.html";
}
else if (data.d == "NotAllowed") {
//Stops the state request and keeps you on the state you already are
e.preventDefault();
} else {
//Proceed to load the requested state
}
})
.error(function (data, status) {
alert(data.Message);
});
});
I need to resolve the success part before the $stateChangeStart is loaded and can't figure out how to do it.
Is there any way of achieving this?
EDIT
So I have my code like this
.state('myState', {
url: "/myState",
templateUrl: 'views/template.html',
controller: 'ctrlTemplate',
viewId: 99,
resolve: {
fn: function ($stateParams, myFactory) {
myFactory.checkSession({viewId:99})
.then(function onSuccess(response) {
var data = response.data;
if (data.d == "Expired") {
throw "Expired";
}
else if (data.d == "NotAllowed") {
throw "NotAllowed";
} else {
//RETURN to chain data
return data;
//Proceed to load the requested state
}
})
.catch(function (response) {
//e.preventDefault();
});
}
}
})
The $http .then
function is still resolving AFTER $stateChangeStart
and $transitionStart
happens and the new state is already loading. I can see it happening in the browser console debugger
Plz help.