Created
April 7, 2015 16:32
-
-
Save JonMidhir/4cb4400b95669720b5f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Set up the current user | |
app.services.factory('CurrentUser', function(User) { | |
var bootstrapCurrentUser = function() { | |
var data = JSON.parse(currentUser); | |
var user = new User(data); | |
return user; | |
} | |
return bootstrapCurrentUser() || User.me(); | |
}); | |
// The User resource | |
app.services.factory('User', function($resource) { | |
return $resource('/users/:id.json', {}, | |
{ | |
me: { | |
method: 'GET', | |
params: {id: 'me'} | |
} | |
}); | |
}); | |
// Wait for current user load to complete using $promise, before | |
// alerting the user's age. | |
CurrentUser.$promise.then(function(data) { | |
alert("User is " + data.age + " years old!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123})
.$promise.then(function(user) {
$scope.user = user;
});
https://docs.angularjs.org/api/ngResource/service/$resource#!