Installing model-factory is as easy as getting it from bower.
Alternatively you can download a release manually.
Open Releases
$ bower install angular-model-factory --save
angular.module('myModule', ['modelFactory'])
.factory('PersonModel', function($modelFactory){
return $modelFactory('/api/people');
});
Create a new angular factory and use the provided $modelFactory
service
to define a new model.
The default configuration is as easy as this, but there's more to discover.
You can now start using it, by injecting the previously defined PersonModel
into
your controller, directive or service.
angular.module('app', ['modelFactory'])
// defining a new Person model
.factory('Person', function($modelFactory){
return $modelFactory('/api/people');
})
.controller('PersonController', function(Person){
var vm = this;
vm.peopleList = [];
// events
vm.savePerson = savePerson;
vm.deletePerson = deletePerson;
activate();
////////////////////////////
function activate(){
Person.query().then(function(people){
vm.peopleList = people;
});
}
function savePerson(person){
// person is already an instance of a model factory
// model
person.$save().then(
function(){
console.log('Person successfully saved!');
})
.catch(function(){
console.error('Oops, there was an error saving the person');
});
}
function deletePerson(person){
person.$destroy();
}
});
Angular Model Factory is packed of features, and we're working on providing many more!