Skip to content

Instantly share code, notes, and snippets.

@zackham
Created December 30, 2011 23:49
Show Gist options
  • Save zackham/1542095 to your computer and use it in GitHub Desktop.
Save zackham/1542095 to your computer and use it in GitHub Desktop.
/**
* Get currently logged in user
* @method getCurrent
* @static
* @return {Promise} A promise for the currently authenticated user
*/
User.getCurrent = function() {
if(this.currentDfd)
return this.currentDfd;
var dfd = this.currentDfd = new $.Deferred();
// we might already have a user prepopulated, let's check
if(rwgps.ns('config').currentUser !== undefined) {
if(rwgps.config.currentUser) {
dfd.resolve(new User(rwgps.config.currentUser)); // we have a user
} else {
dfd.reject(); // user is not logged in
}
} else {
$.getJSON(baseURL + '/users/current.js?callback=?', function(data) {
if(data) {
dfd.resolve(new User(data));
} else {
dfd.reject();
}
});
}
return dfd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment