Aspect-Oriented Programming with AngularJS

970 views
Skip to first unread message

Minko Gechev

unread,
Aug 7, 2013, 9:11:55 AM8/7/13
to ang...@googlegroups.com
Hello guys,

I've always made some parallels between SpringSource and AngularJS - at least the DI and abstraction they provide.
The next key aspect from SpringSource which I think might be useful while developing AngularJS applications is the AOP (Aspect-Oriented Programming).

I built a simple AOP framework for AngularJS which can be found at https://github.com/mgechev/angular-aop

I tried to make it with the simplest possible interface, I hope it'll be useful for you. It's totally open for contributions if you have ideas for it!

Best regards,
Minko.

Jaime J. Febres Velez

unread,
Oct 10, 2013, 9:59:10 PM10/10/13
to ang...@googlegroups.com
I was just looking at something like that, will play with it to see if it fits our needs.

Despite the fact if we choose it or not, seems to be quite good, congrats ;)

OpenNota

unread,
Oct 11, 2013, 8:32:46 AM10/11/13
to ang...@googlegroups.com
Interesting.

徐璟

unread,
Sep 17, 2014, 11:56:53 PM9/17/14
to ang...@googlegroups.com
I wanna know whether I can annotate aspects into functions in controllers

在 2013年8月7日星期三UTC+8下午9时11分55秒,Minko Gechev写道:

Minko Gechev

unread,
Sep 18, 2014, 10:39:49 AM9/18/14
to ang...@googlegroups.com
Controllers add methods to the scope associated with them, so basically you can wrap given method by:

myApp.factory('AfterAdvice', function () {
  return function AfterAdvice() {
    console.log('After advice');
  };
});

function MyCtrl($scope, AfterAdvice) {
  $scope.foo = execute(AfterAdvice);
  }).after(function () {
    console.log('Controller function...');
  });
}

Unfortunately you cannot use the `config` callback.

Harshit Rohatgi

unread,
Sep 18, 2014, 1:52:39 PM9/18/14
to ang...@googlegroups.com
HI Minko ,
The examples you gave have an advice which is args free. Is there a way , we can pass the arguments from the pointcut to advice. Essentially I want to log the metrics at the point cut. So i would like to get some state variables from there and send it to my code.

Minko Gechev

unread,
Sep 19, 2014, 4:47:26 AM9/19/14
to ang...@googlegroups.com
Hey Harshit,

yes, it is possible. For advices applied at any pointcut you have access to the arguments of the woven method and for some pointcuts (like after) you have access to the return value as well.

Here is a basic demo: 

// Advice
DemoApp.factory('Logger', function () {
    return function (data) {
      var result = ['Method: ' + data.method,
      'Pointcut: ' + data.when, 'Arguments: ' + data.args.join(', '),
      (data.when === 'After') ? 'Return value: ' + data.result : '']
      console.log(result.join('\n'));
    };
});

// Service, which will be woven with the advice above
DemoApp.factory('DummyService', function () {
  return {
    foo: function () {
      console.log('I\'m in foo!');
    },
    bar: function (arg1, arg2) {
      console.log('I\'m in bar! Arguments:', arg1, arg2);
      return 2;
    }
  };
});

// "Annotation" of the service
DemoApp.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    DummyService: [{
      jointPoint: 'after',
      advice: 'Logger'
    }]
  });
});

// Invocation of the service's method
DemoApp.controller('ArticlesListCtrl', function (DummyService) {
  DummyService.bar(42, 1.618);
});

// Result:
/**************************
Method: bar
Pointcut: After
Arguments: 42, 1.618
Return value: 2 
***************************/

Best,

Juan Castillo

unread,
Jan 27, 2015, 9:41:21 AM1/27/15
to ang...@googlegroups.com
Your framework looks great. How would it handle promises?

Minko Gechev

unread,
Jan 29, 2015, 8:02:32 AM1/29/15
to Juan Castillo, ang...@googlegroups.com

It has:

  • onResolveOf
  • onRejectOf
  • afterResolveOf

pointcuts. There’s an open issue for onNotify, so this feature might be implemented as well in near future.

Best regards,
Minko
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/5dv-LjNRgDc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Juan Castillo

unread,
Jan 30, 2015, 3:17:25 PM1/30/15
to ang...@googlegroups.com, juandavid...@talosdigital.com
I see! That sounds great. Thanks for the quick reply.
Reply all
Reply to author
Forward
0 new messages