InlineInvoker

(This tag must be placed inside an <EventHandlers> tag or a <MessageHandlers> tag)

InlineInvoker allows you to call a method without having to instantiate an object (compare with MethodInvoker). You only need to provide a method to call, which can be a method created in the event map (within a Script block), a static method on a class, or method provided by an already created object or singleton.

Any value returned by the function will be available in the lastReturn. See Using lastReturn and Using Smart Objects (lastReturn)

Example:

<InlineInvoker
   method="methodToExecute"
   arguments="{['argument1', 'argument2']}"/>

Attributes

method

required

The method attribute specifies what function to call. If you had a function called doWork(), you would then write:

<InlineInvoker method="doWork" />

arguments

If the function in your class has arguments, you can pass them via the "arguments" attribute. Suppose your doWork function has the following signature:

public function doWork(name:String, value:Number)

then you can pass those arguments as follows:

<InlineInvoker
   method="doWork"
   arguments="{['Tom', 36]}"/>

Note that the arguments attribute expects an array. See MethodInvoker (arguments section) for more information.

Calling a static method

With the InlineInvoker tag, you can call static methods. For example, you could call the function random() of the Math library:

<InlineInvoker method="Math.random" />

Remember that any value returned by the function will be available in the lastReturn (Using lastReturn).

Calling a local method

If you have created a method in your event map, you can call it with the InlineInvoker tag:

<mx:Script>
private function myMethod():void {
   trace('you called me!');
}
]]>
</mx:Script>

<EventHandlers type="myEventType">
   <InlineInvoker method="myMethod" />
</EventHandlers>

Calling a method on a singleton class

Suppose you have a singleton class that contains the method "myMethod" and that you access the singleton instance by the getInstance() method. Then you could call your the method by:

<InlineInvoker method="{MySingleton.getInstance().myMethod}" />

11 responses

  1. hi,

    how can i indicate on which object should be invoked?
  2. That object will have to be accessible in the event map, so that you can call it by:
    <code>
    <InlineInvoker method="{myObject.myMethod}" />
    </code>
  3. Laura,

    Could you be more specific from your last response? I'm mostly following what you said, but am unclear on what you mean by "object will have to be accessible in the event map". Do you mind providing a more detailed example. Thanks.
  4. The event map is like any other mxml file. If you want to call a function on "myObject", myObject has to be declared as a variable within a script block or in tag.
  5. Hi!

    What if I need to receive a response from the called method? For instance, the method returns a Boolean value?

    Thanks and Happy New Year!
  6. Returned values as available as lastReturn. See link in the second paragraph of this page.
  7. Laura, thanks for your quick answer. My bad: the solution was in front of my eyes! =/ Silly boy!
  8. Exactly what I need to call a static method...sweet!
  9. The above comments and documentation still has me confused.

    In my main I have:
    <comp:DTV id="dataTable" ...>

    Inside the DTV is a method:
    private function export():void {}

    In the eventmap I want to have my event handler call the method but it never compiles, here is what we tried:

    <Script..
    import mx.core.FlexGlobals;
    private var dtv:DataTableView = FlexGlobals.topLevelApplication.DataTableView;

    <EventHandlers ...
    <InlineInvoker method {dtv.exportTableToDisk}"...

    Can someone give us an answer?
  10. Small update/Note:

    First I did a typo and the function is exportTableToDisk not export and if I make it public not private, it will compile but the end result is:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    on the InlineMethod.
  11. Ray,
    I would recommend you to create a function in your map to make the method call you need and call that method with InlineInvoker.

Comments now closed