Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
angular.module('myApp')
.factory('Model', function Model(Resource) {
function Model(mdl){
var model = mdl || {};
this.attr = model.attr;
}
Model.collection = [];
Model.instance = {};
Model.findCollection = function(params, callback) {
Resource.query(params, function(data) {
Model.collection = data;
callback(data);
});
}
Model.findCustomCollection = function(callback) {
var params = { /* custom params here... */ };
Model.findCollection(params, function(data) {
callback(data);
});
}
Model.find = function(id, callback) {
Resource.get({ id: id }, function(data) {
Model.instance = new Model(data)
return Model.instance;
});
}
Model.prototype.save = function(callback) {
model = new Resource(this.model);
model.$save(callback());
}
Model.prototype.update = function(callback) {
model = new Resource(this.model);
model.$update(callback());
}
Model.prototype.delete = function(callback) {
model = new Resource(this.model);
model.$remove(callback());
}
Model.prototype.customMethod = function(params) {
// code here
}
return Model;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.