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);
}
}
}
})
})(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);
-
2\$\begingroup\$ Please try to explain a bit more about what it is that your code is doing. \$\endgroup\$Simon Forsberg– Simon Forsberg2014年02月01日 18:34:32 +00:00Commented Feb 1, 2014 at 18:34
-
\$\begingroup\$ The code extends a angular service \$\endgroup\$Whisher– Whisher2014年02月01日 19:08:04 +00:00Commented Feb 1, 2014 at 19:08
1 Answer 1
At first sight, your code is short and there is nothing wrong with it.
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
id
inall
since 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.
-
\$\begingroup\$ Thanks for the review but there is a drawback to extend service/factory in angularjs stackoverflow.com/questions/21496331/… \$\endgroup\$Whisher– Whisher2014年02月01日 20:44:28 +00:00Commented Feb 1, 2014 at 20:44
-
\$\begingroup\$ That code works but you should pay attention \$\endgroup\$Whisher– Whisher2014年02月01日 20:55:30 +00:00Commented Feb 1, 2014 at 20:55