mgechev/angular-aop

ngResource gets incorrectly wrapped into a function aspect

valir opened this issue · 2 comments

I'm using angular-aop to put an around advice for the ngResource I use to call server-side API. Like this:

myServices.factory('WelcomeService', [ '$resource', 'execute', 'MyAroundAspect', 
      function($resource, execute, ServerCommState ) {
          return execute(MyAroundAspect).around( $resource('/api/welcome/:id', {
              id: '@id'
          }, {
              create: {
                  method: 'POST',
                  params: {
                      nick: '@nick'
                  }   
              },  
              update: {
                  method: 'PUT'
              }
          }));
      }
  ]);

When calling this service's get method, I get an undefined exception, unless I modify angular-aop.js like this:

  //Builds specified aspect
  AspectBuilder = { 
      buildAspect: function (advice, pointcut) {
        var self = this;
        return function (target, rules) {
          // if (typeof target === 'function') {
          //   return self._getFunctionAspect(target, pointcut, advice);
          // } else if (target) {
            return self._getObjectAspect(target, rules || {}, pointcut, advice);
          // }
        };  
      },  

As you may see, I modify the code so buildAspect always return an object aspect. With this modification, the MyAroundAspect gets called, twice per service call.

My solution is not an ideal one, but I'm not using function aspects, so that's fine for me. However, what should be done in order to enable AOP for ngResource without disabling function advices ?

Thanks for the issue. I'll take a look asap!

I reviewed the issue. Since $resource is a function with methods, the _getFunctionAspect method is being invoked.

I'll extend the API by adding a property, which forces the framework to wrap the function's methods.