Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
Anton Isaykin edited this page Dec 5, 2015 · 9 revisions

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.

Clone this wiki locally