mgechev / aspect.js

JavaScript library for aspect-oriented programming using modern syntax.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not able to call Servlet from Aspect

naimesh-shah opened this issue · comments

I had a issue with my aspect where i am trying to call my servlet from @aftermethod through http.get() call, but _http:Http is not getting initialized in aspect and getting the error,
TypeError: cannot read property 'get' of undefined

I have also tried the approach that you mentioned in the comments of issue 22 (#22), but not able to succeed in that.

@mgechev - Please help how can i call servlet from @aftermethod under LoggerAspect.

Please provide a code snippet so I can try to reproduce the problem.

import {Injectable, Component} from 'angular/core';
import {beforeMethod, afterMethod, Metadata} from 'aspect.js';
import {PolicyService} from 'app/policies/policy.service';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {PolicyListSavingsComponent} from 'app/policies/policy-list-savings/policy-list-savings.component';

@Injectable
export class PolicyAspect{

	policyService: PolicyService;
	
	constructor(private _http:Http, policyService:PolicyService) {}

	@beforeMethod({
		classNamePattern:/^PolicyListSavingsComponent$/,
		methodNamePattern:/loadPolicyDetails$/
	}) 
	beforeMethodExecution(meta: Metadata){
		console.log("Before method execution");
	}
	
	
	@afterMethod({
		classNamePattern:/^PolicyListSavingsComponent$/,
		methodNamePattern:/loadPolicyDetails$/
	}) 
	afterMethodExecution(meta: Metadata){
		console.log("After method execution");
		meta.method.context.policyService._http.get('/v1/systemLogginServlet').map((response: Response) => {console.log(response.json()) });
	}
}

The above code is aspect that i had wrote and at @afterMethod, _http is used to call my servlet.
The problem is that servlet call is not happening, i am able to get the http object at this place.
Please suggest on the same.

You can't use Angular's DI in aspects.

So is there any work around for this or is there any way i can call to my servlet from aspect?

You can access the context into which the aspect is being invoked. If you expose the http service as a property you'll be able to make requests.

Thank you for your response :) , its working now.