Created
August 20, 2013 16:38
-
-
Save alonextou/6283925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.AuthController = Ember.Controller.extend({ | |
// isGuest: true, | |
observeIsGuest: function() { | |
console.log('isGuest changed: ' + this.isGuest); | |
}.observes('isGuest'), | |
login: function() { | |
self.set('isGuest', false); | |
}); | |
}, | |
logout: function() { | |
self.set('isGuest', true); | |
}, | |
checkLogin: function() { | |
var self = this; | |
var response = $.Deferred(); | |
$.ajax({ | |
url: '/users/me', | |
type: 'GET' | |
//contentType: 'application/json', | |
}).done(function(data) { | |
response.resolve(data); | |
console.log('done checking'); | |
if (typeof data === 'undefined') { | |
self.set('isGuest', true); | |
} else { | |
self.set('isGuest', false); | |
} | |
}).fail(function(xhr, status, error) { | |
console.log(xhr.responseText); | |
response.reject(); | |
}); | |
return response.promise(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.ApplicationRoute = Ember.Route.extend({ | |
beforeModel: function(transition) { | |
this.controllerFor('auth').checkLogin().done(function(){ | |
transition.retry(); | |
}); | |
} | |
}); | |
App.GuestRoute = Ember.Route.extend({ | |
redirect: function() { | |
if(!this.controllerFor('auth').get('isGuest')){ | |
this.transitionTo('index'); | |
} | |
} | |
}); | |
App.AuthRoute = Ember.Route.extend({ | |
redirect: function() { | |
if(this.controllerFor('auth').get('isGuest')){ | |
this.transitionTo('index'); | |
} | |
} | |
}); | |
/* Guest Routes */ | |
App.LoginRoute = App.GuestRoute.extend({ | |
renderTemplate: function() { | |
this.render({ controller: 'auth' }); | |
}, | |
setupController: function(controller, model) { | |
this.controllerFor('auth').reset(); | |
}, | |
}); | |
/* Auth Routes */ | |
App.LogoutRoute = App.AuthRoute.extend({ | |
setupController: function(controller, model) { | |
this.controllerFor('auth').logout(); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment