Skip to content

mgechev/angular-aop

Repository files navigation

Travis CI Join the chat at https://gitter.im/mgechev/angular-aop

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.

About

Library for aspect-oriented programming with AngularJS.

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published