Skip to content

roypeled/angular-class-extender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

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>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js"></script>
    <script src="angular-class-extender.js"></script>
</head>
<body ng-app="app">
...
angular.module("app", ["ngExtender"]);

Basic Inheritance

function ParentClass($scope){
  $scope.foo = function(){
    // Do A
  }
}

function HelperClass($scope){
  $scope.help = function(){
    // Get help!
  }
}

function ChildClass($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

function ChildClass($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

function ParentClass($scope){
  $scope.$abstract("bar");
}

function ChildClass($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.

function ParentClass($scope){
  window.alert($scope.bar); // undefined

  $scope.$binded(function(){
    window.alert($scope.bar); // hello world!
  });
  
  $scope.$abstract("bar");
}

function ChildClass($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(){

    var scope = $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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published