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);
\$\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\$
    – Whisher
    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.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.