TL;博士?如果你想去(几乎)工作代码,我在这里做了一个 jsFiddle 。
假设我有一个如下所述的 Ember 路由器。我想让它管理当前用户身份验证的状态。是否可以取消状态转换?
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()
})
})
});