I created a factory for getting an array of Messages from my server. It looks kind of like this:
app.factory('messagesService', [
'$resource',
function($resource) {
var self = this;
self.service = $resource('/messages');
self.all = function() {
return self.service.query();
};
return {
all: self.all
};
}
]);
This way I can run messagesService.all() from other controllers.
However this is returning the raw data from the server, and I would like to do some processing the data first. I am new to Angular and trying to understand the right way of doing things.
I would like to have my service return a more complete Message object.
I was thinking I would create a function and include it in the service file like this:
self.Message = function(data) {
var self = this;
self.author = data.author;
self.message = $sce.trustAsHtml(data.message);
};
Then in the .query() I could create new Message(data) objects and return an array of those. Is that the right way to do this? Should my self.Message be encapsulated in a separate model file?