Using Smart Objects

The EventMap tag provides a set of Smart Objects that can be used within the EventHandlers and MessageHandlers tags. These objects expose data such as the current event, the value returned by a MethodInvoker or the result of a server call.

Examples of usage can be found in the documentation for MethodInvoker, EventAnnouncer and the other tags that can be used inside the EventHandlers and MessageHandlers.

These smart objects are specified by using bindings, but it is important to note that this binding is executed only once, at the beginning of the application.

event

Available only inside the EventHandlers tag. It refers to the event that made the list of handlers execute. The event itself or properties of the event can be used as arguments of MethodInvoker methods, service methods, properties for any of the handlers, etc.

For example, a method called by a MethodInvoker can receive event properties as arguments, or the event itself:

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

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

</EventHandlers>

message

Only available inside a MessageHandlers tag. It refers to the message received that made the list of handlers execute. The message itself or properties of the message can be used as arguments of MethodInvoker methods, service methods, properties for any of the handlers, etc.

For example, a method called by a MethodInvoker can receive message properties as arguments, or the message itself:

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

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

</MessageHandlers>

resultObject

Only available inside a resultHandlers inner tag. It refers to the result returned by a service that made the result handlers execute. The result itself or properties of the result can be used as arguments of MethodInvoker methods, service methods, properties for any of the handlers, etc.

For example, a method called by a MethodInvoker can receive result properties as arguments, or the result itself:

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

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

</resultHandlers>

See more information about resultHandlers inner tag at: "Handling a service result or fault".

fault

Only available inside a faultSequence inner tag. It refers to the fault returned by a service that made the sub sequence execute. The fault itself or properties of the fault can be used as arguments of MethodInvoker methods, service methods, properties for any of the handlers, etc.

For example, an object instantiated by a MethodInvoker can receive fault properties as arguments, or the fault itself:

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

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

</faultHandlers>

See more information about faultHandlers inner tag at: "Handling a service result and fault".

lastReturn

lastReturn is always available, although its value might be null. It typically represents the value returned by a method call made by a MethodInvoker or InlineInvoker, but other handlers might also return a value, such as:

  • token: returned by RemoteObjectInvoker, WebServiceInvoker and HTTPServiceInvoker (value is returned before call result is received)
  • boolean value: returned by EventAnnouncer after dispatching the event. True for successful dispatch, false for unsuccessful (either a failure or when preventDefault() was called on the event).

All other handlers will nullify any previous value.

If that returned value is an object, you can access specific properties with the same syntax used by other smart objects: lastReturn.myProperty

See "Using lastReturn" in the MethodInvoker tag documentation.

data

Every EventHandlers and MessageHandlers block contain a placeholder object called "data". This object can be used to store temporary data that many tags in the list can share.

For example, a function called by a MethodInvoker can receive data properties as arguments, or the data itself:

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


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

scope

Every handlers list contains an object (IScope) that represents the running list's scope. As an IScope, you can use it to stop the currently running list. If you cast it to Scope, for example, you can also access an event dispatcher, the current event and other properties. This scope can be used by functions called by MethodInvokers.

Smart Objects limitations

SmartObject cannot be casted. For example, if you are expecting a custom event MyEvent, you cannot cast the event SmartObject to your custom class. Therefore, this is not allowed: arguments="{[MyEvent(event).username]}"

SmartObject cannot be concatenated because of its lazy initialization. Therefore, the following is not allowed: arguments="{['Hello ' + event.name + '!']}"

4 responses

  1. Strange .

    in above example.

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

    event properties can be used in methodInvoker tag.

    but why it cannot successfully used in HttpServiceInvoker Request Properties.
    In your HTTPServiceInvoker document page. if I have only one of event.userName , It is ok , If I have more than one , then I can only get first event properties.

    Is this a bug ?
  2. Johnny,
    That works, see FlickrBook example. It sends multiple parameters as in:
    <Request method="flickr.photos.search" api_key="{Config.API_KEY}" user_id="{event.userID}" tags="{event.tags}" page="{event.page}" />
  3. I'm more interested in the question of how do you attach information to the token for retrieval later, as you often need to do if you make several requests at one time that will be handled by the same handler, sort of like this: http://flexdiary.blogspot.com/2008/11/more-thoughts-on-remoting.html
  4. Amy,
    The best way to pass anything to the handlers is via the event or in some other way to the handling <MethodInvoker>. Anything within the handlers will keep its state, even during async calls, so anything you store there, will be accessible in the result, ie: the data object, the lastReturn object, etc
    For example, let's say that you wanted to pass the user id that was sent in the original event to the result handler:

    <RemoteObjectInvoker ....>
    <resultHandlers>
    <MethodInvoker method="handleResult" arguments="{[event.userId, resultObject]}"/>
    </resultHandlers>
    </RemoteObjectInvoker>

    In any case, the token is returned to you as lastReturn, so you can do anything you need with it.

Comments now closed