As usual I'm wondering if there is a better way to do:
(The code extends a angular service)
(function(window, angular, undefined) {
'use strict';
angular.module('api.base', ['restangular'])
.factory('Base', function(Restangular) {
return function(route){
var elements = Restangular.all(route);
return {
one : function (id) {
return Restangular.one(route, id).get();
},
all : function (id) {
return elements.getList();
},
store : function(data) {
return elements.post(data);
},
copy : function(original) {
return Restangular.copy(original);
},
getElements : function() {
return elements;
}
}
}
})
})(window, angular);
(function(window, angular, undefined) {
'use strict';
angular.module('api.post', ['api.base'])
.factory('Post', function(Base) {
function ngPost() {
this.prop = ['publish','draft'];
this.myMethod = function(){}
};
return angular.extend(Base('post'), new ngPost());
})
})(window, angular);
Update nice :)
(function(window, angular, undefined) {
'use strict';
angular.module('nodblog.api.comment', ['nodblog.api.base'])
.config(function(RestangularProvider) {
RestangularProvider.setRestangularFields({
id: "_id"
});
})
.factory('Comment', function(Base) {
function NgComment() {
this.byPostId = function(id){
return this.getElements().one('post',id).getList();
}
};
return angular.extend(Base('comment'), new NgComment());
});
})(window, angular);