Seeking a way to retrieve objects from a server, similar to this:
var User = $resource('/user/:userId', {userId:'@id'});
I desire all User
instances to possess specific methods for computing derived data without requiring the server to provide it. For instance, given:
var aUser = User.get({userId: 43});
If the server responds with:
{id: 43, name: "Bob", alertTimestamp: 1447365544}
I would like the ability to execute:
if (aUser.alertTimePassed()) {
// perform actions
}
Is there a more elegant method to achieve this goal rather than resorting to something that feels like a workaround?
var alertTimePassed = function () {
var now = (new Date()).getTime() / 1000;
return now >= this.alertTimestamp;
};
var User = $resource('/user/:userId' , {userId: '@id'} ,{
get: {
method: "GET",
url: '/user/:userId',
transformResponse: [angular.fromJson, function (obj) {
obj.alertTimePassed = alertTimePassed;
}]
}
});