GithubHelp home page GithubHelp logo

willmendesneto / angular-class-extender Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roypeled/angular-class-extender

0.0 2.0 0.0 252 KB

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

License: MIT License

angular-class-extender's Introduction

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){
  $extend($scope).with(ParentClass);
  
  $scope.foo = function(){
    
    // Do B
    
    $scope.$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!"
}

angular-class-extender's People

Contributors

roypeled avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.