views:

54

answers:

2

Hi,

I am working on a Flex Front End at the moment, and have been using the Parsley framework for passing messages/events around.

I was wondering if there is a simple way for a function (in this case, an event's constructor) to obtain a reference to the object which called it?

This is to ensure that a certain event that I am defining can only be dispatched by one specified class. My thinking is to check the caller of the constructor somehow, and throw an error if it is not of the correct type.

I am open to suggestions of alternative approaches here, but I would ideally like to stick to using the Parsley 'MessageHandler' approach if at all possible.

Thanks for reading guys..

A: 

Disclaimer: I'm not familiar with Parsley.

See stack trace in actionscript 3. You can get the caller function from the stack trace.

There is one thing though.. The stack trace can only be detected in debug mode and to my knowledge there is no way to get the caller in "production" mode.

mkorpela
That's a great link, thanks.
DannyC
A: 

Stack trace works, but you could much more easily pass a reference to the caller to the function and type check it.

function dispatchEvent(caller:*):void {
   if(caller is SpecificClass) {
      // then dispatch event
   }
}
stochastic
I'm also not familiar with Parsley btw.
stochastic
Thanks. Yeah I did think about this, but I was looking for something more 'native' I guess. I may just go with something like this.
DannyC