patternjavascriptMinor
Angularjs extends service
Viewed 0 times
extendsangularjsservice
Problem
As usual I'm wondering if there is a better way to do:
(The code extends a angular service)
(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);
}
}
}
})
})(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);Solution
At first sight, your code is short and there is nothing wrong with it.
At second sight, there are a few things to improve:
These things I gleaned from JSHint, you should use it.
Furthermore, I could be wrong, it seems that you are mixing functions which I would put in
At second sight, there are a few things to improve:
- Your semicolons are all over the place; you have both missing and pointless semicolons.
- There is no point in declaring
idinallsince you clearly will not use it
- ngPost is an old skool constructor, it's name really should start with an uppercase
These things I gleaned from JSHint, you should use it.
Furthermore, I could be wrong, it seems that you are mixing functions which I would put in
posts together with functions which I would put in post. I am not sure that is the best approach.Context
StackExchange Code Review Q#40628, answer score: 6
Revisions (0)
No revisions yet.