5
\$\begingroup\$

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);
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 1, 2014 at 18:25
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Please try to explain a bit more about what it is that your code is doing. \$\endgroup\$ Commented Feb 1, 2014 at 18:34
  • \$\begingroup\$ The code extends a angular service \$\endgroup\$ Commented Feb 1, 2014 at 19:08

1 Answer 1

6
\$\begingroup\$

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 in all 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.

answered Feb 1, 2014 at 20:27
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the review but there is a drawback to extend service/factory in angularjs stackoverflow.com/questions/21496331/… \$\endgroup\$ Commented Feb 1, 2014 at 20:44
  • \$\begingroup\$ That code works but you should pay attention \$\endgroup\$ Commented Feb 1, 2014 at 20:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.