Skip to content

Instantly share code, notes, and snippets.

@JonMidhir
Created April 7, 2015 16:32
Show Gist options
  • Save JonMidhir/4cb4400b95669720b5f0 to your computer and use it in GitHub Desktop.
Save JonMidhir/4cb4400b95669720b5f0 to your computer and use it in GitHub Desktop.
// 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!");
});
@jonwiz
Copy link

jonwiz commented Apr 7, 2015

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#!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment