0 Replies Latest reply on Apr 4, 2007 7:07 AM by ellery.chan

    ExternalInterface: pass object reference across interface - how?

    ellery.chan
      I want to invoke methods on specific Javascript or ActionScript objects through calls across the ExternalInterface barrier. I would like to be able to do this either AS -> JS or JS -> AS.

      So I would like to pass an object reference across the interface. I'm not sure exactly what *is* getting passed (is it the serialized value of the object?), but it doesn't seem to work as an object reference.

      Here's a notional code sample. I have two symmetric object definitions, one in Javascript, one in ActionScript. During initialization, one instance of each object will be created and given a pointer to the other. Then they should each be able to invoke a method on the other to "do stuff".

      //----------[ code ]---------------------------------
      //--- Javascript ---
      class JSobj {
      var _asObj;
      }
      JSobj.prototype.setASObj = function(obj) { _asObj = obj; }
      JSobj.prototype.callASObj = function(foo) { callASObjectMethod(_asObj, foo); } // does: _asObj.asMethod(foo);
      JSobj.prototype.jsMethod = function(bar) { /* do stuff */ }
      function callJSObjectMethod(obj, args) { obj.jsMethod(args); }

      //--- ActionScript ---
      class ASobj {
      var _jsObj;

      public function set jsObj (obj:Object):void { _jsObj = obj; }
      public function callJSObj (foo:Number):void { ExternalInterface.call("callJSObjectMethod", _jsObj, foo); } // does: _jsObj.jsMethod(foo);
      public function asMethod (bar:Number):void { /* do stuff */ }
      }
      function callASObjectMethod (obj:Object, args) { obj.asMethod(args); }
      ExternalInterface.addCallback("callASObjectMethod", callASObjectMethod);
      //----------[ /code ]---------------------------------

      My workaround is to pass a uint as an opaque handle for the object, then resolve it when it is passed back. I'd rather pass a real reference if possible. Is there a way to pass object references between JS and AS?

      Thanks,
      -e-