Variable-length argument passing and dynamic method invocation within flex / djangoamf

By galactivision

Things get slightly tricky when attempting to dynamically invoke methods on a proxied object, as I discovered. When attempting an AS2-style fix, I also discovered that syntax has changed.

Trapping undefined method calls used to be achieved with the Object.__resolve property in AS2, but this has changed in AS3 / Flex2:

“The Proxy class, which is the replacement for the Object.__resolve property from ActionScript 2.0, allows you to intercept references to undefined properties or methods before an error occurs. All of the methods of the Proxy class reside in the flash_proxy namespace in order to prevent name conflicts.” (from LiveDocs: Language and Syntax- namespaces)

Since djangoamf (and most remote service implementations) use this proxy method to allow calls of the form remoteService.remoteMethod(args) one must perform a little trickery if writing a wrapper method to call the remote service that is itself dynamically invocated. The following method I wrote to wrap around invocations of the remote service; it takes the remote method name and a variable number of arguments, then dynamically invokes the requested method on the proxied service (interesting/relevant items are listed in bold):

(script)

import flash.utils.Proxy;
.
.
.
public function loadRemoteData(callbackObj:*, callbackname,
  methodName, ...args) : void {

    this.callbackObj  = callbackObj;
    this.callbackName = callbackName;

    // place the remote method name
    // at front of passed-in arguments
    args.unshift(methodName);

    // directly call proxy::callProperty
    // bypassing normal dynamic invocation
    Proxy(service).flash_proxy::callProperty.apply(service,
      args);

}
.
.
.
private function handleResult(re:ResultEvent) : void {
    callbackObj[callbackName](re);
}

(mxml)

.
.
.
<s2:RemoteService
    id="service"
    gatewayUrl="http://127.0.0.1:8000/gateway/"
    destination="BlogService"
    useAMF0="true"
    result="handleResult(event)"
    fault="handleFault(event)"
    showBusyCursor="false" />

Thanks to www.docsultant.com for an excellent read on some of the more advanced reflection syntax in Flex / Actionscript.

see: www.docsultant.com: flex internals
also: LiveDocs: AS2 -> AS3 migration / language changes

Leave a Reply