A lightweight angular module that adds support for class inheritance and method abstraction.
Here is the DEMO
This module will help you minimize code duplication by classic methods of inheritance.
To use this module you must add the JS file to your HTML and add ngExtender as a dependency to your app:
<!DOCTYPE html><html><head><title></title><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js"></script><scriptsrc="angular-class-extender.js"></script></head><bodyng-app="app">
...angular.module("app",["ngExtender"]);functionParentClass($scope){$scope.foo=function(){// Do A}}functionHelperClass($scope){$scope.help=function(){// Get help!}}functionChildClass($extend,$scope){$extend($scope).with(ParentClass,HelperClass);}Now calling foo() or help() from ChildClass would actually call foo() from ParentClass and do 'A' or help() from HelperClass.
functionChildClass($extend,$scope){var$super=$extend($scope).with(ParentClass);$scope.foo=function(){// Do B$super.foo();}}Calling foo() from ChildClass would perform the overidden method, and the ParentClass method by calling $super.
functionParentClass($scope){$scope.$abstract("bar");}functionChildClass($extend,$scope){$extend($scope).with(ParentClass);$scope.bar=function(){// Do C}}Using the $abstract syntax you are making sure that whichever class extended ParentClass it has to implement bar() method.
Not doing so would result in an error:
Abstract method bar must be overriden in ChildClass!
This handler is called after all the classes in your inheritance tree have been linked, so you can be sure you can access all of the inherited or implemented properies.
functionParentClass($scope){window.alert($scope.bar);// undefined$scope.$binded(function(){window.alert($scope.bar);// hello world!});$scope.$abstract("bar");}functionChildClass($extend,$scope){$extend($scope).with(ParentClass);$scope.bar="hello world!"}Angular Class Extender now fully supports unit testing by using the injected class $extendedController instead of $controller
it("should properly identify extended methods",function(){varscope=$rootScope.$new();scope.test=true;$extendedController("ChildClass",{$scope: scope});expect(scope.test).toEqual(true);expect(scope.bar).toEqual("hello world!");expect(scope.foo).toBeDefined();});