Git Product home page Git Product logo

angular-aop's Introduction

[![Join the chat at https://gitter.im/mgechev/angular-aop](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mgechev/angular-aop?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Online demo

If you prefer learning by doing (trial and error), come right this way.

API

AngularAOP allows you to apply aspect-oriented programming in your AngularJS applications.

If you notice that you have cross-cutting concerns you can isolate them in individual services and apply them as follows:

myModule.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    ServiceToWove: [{
      jointPoint: JOINT_POINT,
      advice: ADVICE_NAME,
      methodPattern: /Special/
      argsPattern: [/arg1/, /arg2/]
    }, {
      jointPoint: JOINT_POINT
      advice: ADVICE_NAME
    }]
  });
});

The joint-points supported in the current version of the framework are:

  • after - the advice will be invoked after the target method
  • afterResolveOf - the advice will be invoked after the promise returned by target method has been resolved and after the resolve callback attached to the promise is invoked
  • aroundAsync - the advice will be invoked before the target method was invoked and after the promise returned by it was resolved
  • around - the advice will be invoked before the target method was invoked and after it was invoked
  • beforeAsync - the target method will be called after the promise returned by the advice was resolved
  • before - the target method will be invoked after the advice
  • onRejectOf - the advice will be invoked when the promise returned by the target method was rejected
  • onResolveOf - the advice will be invoked after the promise returned by the target method was resolved but before the resolve callback attached to the promise is invoked
  • onThrowOf - the advice will be called if the target method throws an error

For additional information about Aspect-Oriented Programming and AngularAOP visit the project's documentation and this blog post.

Change log

v0.1.0

New way of annotating. Now you can annotate in your config callback:

DemoApp.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    ArticlesCollection: {
      jointPoint: 'before',
      advice: 'Logger',
      methodPattern: /Special/,
      argsPatterns: [/arg1/, /arg2/, ..., /argn/]
    }
  });
});

v0.1.1

Multiple aspects can be applied to single service through the new way of annotation:

DemoApp.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    ArticlesCollection: [{
      jointPoint: 'before',
      advice: 'Logger',
    }, {
      //aspect 2
    }, {
      //aspect 3
    }, ... , {
      //aspect n
    }]
  });
});

Note: In this way you won't couple your target methods/objects with the aspect at all but your target service must be defined as provider.

v0.2.0

Added forceObject property to the rules. This way issues like #12 will not be reproducable since we can force the framework to wrap the target's method, insted of the target itself (in case the target is a function with "static" methods").

Issues fixed:

  • Once a function is wrapped into an aspect its methods are preserved. We add the target to be prototype of the wrapper, this way using the prototype chain the required methods could be found.

v0.2.1

Added tests for:

  • Before async joint-point
  • On resolve joint-point

Add JSCS and update Gruntfile.js

v0.3.1

  • deep config property, which allows adding wrappers to prototype methods
  • Fix forceObject

v0.3.1

  • Wrap the non-minified code in build in IIFE (Issue 15)
  • Single 'use strict'; at the top of the IIFE

v0.4.0

  • Add the joint-point names as constants to the executeProvider, so now the following code is valid:
myModule.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    ServiceToWove: [{
      jointPoint: executeProvider.ON_THROW,
      advice: ADVICE_NAME,
      methodPattern: /Special/
      argsPattern: [/arg1/, /arg2/]
    }, {
      jointPoint: executeProvider.BEFORE,
      advice: ADVICE_NAME
    }]
  });
});
  • Add more tests

v0.4.1

  • Special polyfill for IE9 of Object.setPrototypeOf.

Roadmap

  1. joinpoint.proceed()
  2. Use proper execution context inside the target services. This will fix the issue of invoking non-woven internal methods.
  3. More flexible way of defining pointcuts (patching $provide.provider might be required)

Known issues

Circular dependency

This is not issue in AngularAOP but something which should be considered when using Dependency Injection.

Note that if the $injector tries to get a service that depends on itself, either directly or indirectly you will get error "Circular dependency". To fix this, construct your dependency chain such that there are no circular dependencies. Check the following article, it can give you a basic idea how to procceed.

Contributors

mgechev david-gang Wizek slobo christianacca peernohell
mgechev david-gang Wizek slobo christianacca peernohell

License

AngularAOP is distributed under the terms of the MIT license.

angular-aop's People

Contributors

bitdeli-chef avatar christianacca avatar david-gang avatar gitter-badger avatar mgechev avatar peernohell avatar slobo avatar wizek avatar

Watchers

 avatar  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.