Created
March 2, 2014 06:19
-
-
Save DavidQL/9302697 to your computer and use it in GitHub Desktop.
CurrentUserHelper - Ember.js
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
// 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