2

I'd like to know how would be a good way to store data in angular.js that will be accessed across the app from different controllers. I'm using restangular to access the data on the backend. I have my own services to access the data, something like this:

app.factory('models.user', function(Restangular) {
  var users;
  users = {};
  return {
    get: function(user_id) {
      return users[user_id] || (users[user_id] = Restangular.one('users', user_id).get());
    }
  };
});

For all models I define a service to access it. Using this, from any place of the app when I call User.get(1) I get the same model instance.

I'd like to find a better way to do so. What do you guys do in to keep the same instances in your Angular.js apps?

acroca
  • 186
  • 1
  • 6

1 Answers1

0

This is the pretty best way. Why do you confused by this code?

huston007
  • 1,972
  • 1
  • 16
  • 13
  • I have many models, having this all around the code looks a bit ugly. I feel it's something that can be generalized somehow but not sure how. – acroca Oct 21 '13 at 13:54