1

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?

1 Answer 1

1

Check out the latest version of $resource. You can register a "transformResponse" function which -I suspect- is what you want. Angular 1.2 has a lot of great features and it worths upgrading to it (sooner or later it will become a stable release).

BTW, I don't get why you use

var self = this;

Isn't that:

app.factory('messagesService', ['$resource', function($resource) {
    var messages = $resource('/messages'); // extra config necessary?
    return {
      all: messages.query
    };
}]);

good enough? The service API remains the same: messagesService.all().

Sign up to request clarification or add additional context in comments.

3 Comments

I reduced my factory to just return a $resource object. It has a query method that includes transformResponse. However the transformResponse(data) part is just a string of my JSON return from the server. Am I asking for it wrongly? It seems like I should have a more significant place to put my model logic than in the query service. I can't help but think I've got the wrong perspective on this.
I don't think you call it wrongly. transformResponse is pretty low-level but I can't think of any other method to return "model-enhanced" javascript objects from a $resource. Either deserialize with angular.fromJson and do your transformation or skip the whole $resource thing and go with $http and $q promises. Maybe if you read $resource source code you'll find something not very commonly used.
Is there a common pattern for pulling server data in Angular and adding custom methods to the return? It seems like any web-app would need to do that for every model it used. I still feel like I'm missing something horribly obvious or using the wrong words to ask the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.