Home - swimlane/angular-model-factory GitHub Wiki

modelFactory is a light-weight model layer that bridges the gap between some of the features that are common with SPA.

The idea is to encapsulate the logic for communicating with a single REST endpoint into a single Angular service or Model class. A new model is configured like

angular
  .module('myModule', ['modelFactory'])
  .factory('PersonModel', function($modelFactory){
      return $modelFactory('/api/people');
  });

Within some other Angular component (like a service, controller, directive,...) you can then get a reference to the model

function PersonController(PersonModel) {
   var vm = this;
   vm.people = [];

   PersonModel.query().then(function(result) {
      // you'll get a list of people
      vm.people = result;
   });
}

The interesting part is that you'll not only get simple JavaScript objects back from the server, but they're being automatically wrapped as instances of PersonModel. This allows for further conveniences:

...
var somePerson = vm.people[0];
somePerson.name = 'Juri';

// save it back to the server
somePerson.$save();

Interested? Then continue checking out our wiki articles for more details.