Skip to content

Instantly share code, notes, and snippets.

@DavidQL
Created March 2, 2014 06:19
Show Gist options
  • Save DavidQL/9302697 to your computer and use it in GitHub Desktop.
Save DavidQL/9302697 to your computer and use it in GitHub Desktop.
CurrentUserHelper - Ember.js
// define
App.CurrentUserHelper = {
beforeModel: function() {
if (!this.controllerFor('application').get('currentUser')) {
var auth_deferred = $.get(App.Host + '/session');
auth_deferred.then(function(user) {
this.controllerFor('application').set('currentUser', user);
}.bind(this));
return auth_deferred;
}
},
currentUser: function() {
return this.controllerFor('application').get('currentUser');
}.property()
}
// use it in an Ember.Route
App.NewPostRoute = Ember.Route.extend(App.CurrentUserHelper, {
model: function() {
if (!this.get('currentUser')) {
this.transitionTo('posts');
return;
}
return this.store.createRecord('post');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment