Angulars $resource is equipped with a fantastic feature that allows objects to automatically update views by injecting values. Inside the $resource service lies a Resource object where the resolved result is copied using shallowClearAndCopy().
I am interested in chaining the promise returned from a $resource to achieve something similar to having the deferred result injected into a Resource object.
var otherProperty = 'somethingChanging';
var lazyUser = $resource('/user/:userId').get({id:id});
var userDisplayNamePromise = lazyUser.$promise.then(transformResult);
function transformResult(user){
return { displayName: user.firstname + ' ' + user.lastname,
other: user[otherProperty]
};
}
Currently, I have to manually update the display user like this:
userDisplayNamePromise.then(updateDisplayUser);
function updateDisplayUser(displayName){$scope.user = displayName;}
But ideally, I would prefer to do something like:
$scope.user = something(userDisplayNamePromise);
I have not found an easy way to handle promises in this manner. Does anyone have any helpful suggestions?
I have included a basic working example on github.com/burka/resolvling. However, it's hard to believe that nobody has done this before?