HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Angularjs extends service

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
extendsangularjsservice

Problem

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);

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:

  • 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.

Context

StackExchange Code Review Q#40628, answer score: 6

Revisions (0)

No revisions yet.