TL;DR? I made a jsFiddle here if you want to go to (nearly) working code.
Suppose I have an Ember router described below. I'd like to have it manage the state of the current user's authentication. Is it possible to cancel a state transition?
App.Router = Ember.Router.extend({
init: function() {
this.set('authenticated', false);
return this._super();
},
/*
* "Authentication" method - just toggle the state
*/
toggleAuthentication: function() {
var auth = this.get('authenticated');
this.set('authenticated', !auth);
if (auth) {
this.transitionTo('root.home');
} else {
this.transitionTo('loggedIn.home');
}
},
/*
* Root state
* Logged out state tree
*/
root: Ember.State.extend({
home: Ember.State.extend()
}),
/*
* Authenticated state tree
*/
loggedIn: Ember.State.extend({
/* Enter checks user is authenticated */
enter: function(manager, transition, async, resume) {
if (manager.get('authenticated')) {
// proceed
} else {
// cancel the transition & redirect to root.home
}
},
/* Exit sets authenticated to false just to be sure */
exit: function(manager, transition, async, resume) {
manager.set('authenticated', false);
},
/* Sub-states */
home: Ember.State.extend(),
news: Ember.State.extend({
list: Ember.State.extend()
})
})
});