11 3 / 2014

Implementing angular service inheritance is a great step towards minimizing excessive code. The implementation I use, abstracts the functionality of communicating with an api to a base service, then uses child services to define how to communicate. Here is an example.

Base Service

angular.module('ngApp')
.service('Base', function Base($rootScope, $http, $q) {
    function Base(config) {
        this.create(){}
        this.update(){}
        this.delete(){}
        this.all(){}
        // ...
    }
    return Base;
});

Child Service

 angular.module('ngtottsApp')
.service('Category', function Category(Base) {
    function Category() {
        this.id = "";
        this.title = "";
        Category.prototype = new Base({
            class: 'category',
            rules: ['title', 'categoryId']
        });
        return new Category();
});