Skip to content

Latest commit

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

angular-class-extender

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.

Usage

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

Basic Inheritance

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.

Overriding Methods and Calling Super Methods

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.

Implementing Abtract Methods

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!

Binded Event

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!"}

Unit Testing

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

About

A lightweight angular module that adds support for multiple class inheritance and method abstraction.

Resources

Stars

29 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages