Passing Arguments

Some Mate tags allow you to pass arguments to method calls (via the "arguments" attribute) or to the class constructor (via the "constructorArguments" attribute). The values for those arguments can come from a variety of sources, depending on the context the tags is in (ie: inside resultHandlers, Injectors, etc). These arguments are passed as an array.

Suppose you have a method with the following signature (or suppose this method is a constructor):

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

The following examples show how you can pass arguments to that method or constructor

Literal values

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

Event properties

You can pass values coming from the event that triggered the execution of the list of handlers when the tag is placed inside EventHandlers or from the creation event that triggered the execution of the list of actions declared inside an Injectors tag:

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[event.userName, event.age]}"/>

This assumes that the event contained a userName property and an age property.

Service result properties

If the tag is inside a <resultHandlers> tag that originated from a <WebServiceInvoker>, <HTTPServiceInvoker> or <RemoteObjectInvoker> tag, you can also pass values coming from the server result:

<RemoteObjectInvoker .....service attributes....>

   <resultHandlers>
   
      <MethodInvoker
         generator="{MyWorker}"
         method="doWork"
         arguments="{[resultObject.userName, resultObject.age]}"/>


   </resultHandlers>
   
</RemoteObjectInvoker>

Again, this assumes the server returned an object that contained a userName property and an age property.

Service fault properties

If this tag is inside a <faultHandlers> tag that originated from one of the service invoker tags, you can also pass values coming from the server fault. This comes in handy when you want to handle possible server errors.

<RemoteObjectInvoker .....service attributes....>

   <faultHandlers>
   
      <MethodInvoker
         generator="{MyWorker}"
         method="doWork"
         arguments="{[fault.faultDetail, fault.errorID]}"/>


   </faultHandlers>
   
</RemoteObjectInvoker>

Event, service result or fault objects

You can also pass the complete event, resultObject, fault, data or lastReturn as an argument depending on what your function expects.

For example, if the function has the following signature:

public function doWork(event:MyCustomEvent):void

you would be able to send the event as an argument of the method:

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[event]}"/>

If you wanted to send a service result, you would pass the resultObject as an argument.

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[resultObject]}"/>

Your worker function signature would depend on what you are expecting to receive from the server. If you are using Flash Remoting, you might be getting a mapped class, such as a Customer.

public function doWork(customer:Customer)

If you are using HTTPService, you might be getting an XML object:

public function doWork(xmlDoc:XML)

You can also use the service fault, the sequence data or a lastReturn.

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[fault]}" />

Data object

When your tag is inside EventHandlers, there exists a "data" object that can be used to store temporary information. The data is always an object, so your function must accept an object:

public function doWork(data:Object)

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[data]}"/>

You could also use a property on the data object:

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[data.customerId]}"/>

lastReturn

Receiving the value returned by a previous MethodInvoker or ObjectBuilder execution (see Using lastReturn):

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[lastReturn]}"/>

Combining arguments

Of course you can use any combination of arguments:

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{[event.age, resultObject, 'Tom']}"/>

Specifying only one argument

Although the arguments attribute expects an array, it is also possible to supply an object when there is only one argument that the worker function expects. See these examples:

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="A string"/>

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{event}"/>

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{event.userName}"/>

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{resulObject}"/>

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{resulObject.userId}"/>

<MethodInvoker
   generator="{MyWorker}"
   method="doWork"
   arguments="{lastReturn}"/>

Specifying the arguments in verbose mode

Finally, arguments can also be passed by using SmartObjects.

For example, if you want to pass event.userName and event.age, you can pass them as follows:

<MethodInvoker generator="{MyWorker}" method="doWork">
   <arguments>
      <SmartObject source="event" key="userName" />
      <SmartObject source="event" key="age" />
   </arguments>
</MethodInvoker>

1 response

  1. Hi, I would like to know if there is any way to specify a single argument which is an array, and then have Mate interpret that array as a number of arguments...

    for example:
    let event.args = ["arg1", "arg2", "arg3"];

    <RemoteObjectInvoker
    instance="{blah}"
    method="myRpc"
    arguments="{ event.args }" />

    the myRpc function only receives one argument, the args array itself. Ideally I would like Mate to do something more like myRpc("arg1", "arg2", "arg3"). Logically, if one did want Mate to send the array as a single parameter, then the arguments tag would simply need to be in the form:

    arguments="{[ event.args ]}" /> (note the [ ] )

    Please let me know if this is possible and I am simply missing something obvious. Thanks!

Comments now closed