What if you’d like to pass an object to Flex
To continue the topic of my previous post I’m just going to add that there’s a native way to pass objects from JavaScript to Flex’s actionscript.
I’m not going to describe how to structure a object in JavaScript and/or Flex. Let’s go directly to the example.
The function in .js file should return something that is object in JavaScript:
.js file
function getObject() { return { foo : 'bar', zoo : 'tar' }; }
Now, supposing that in Flex there already has been added a callback with ExternalInterface(‘getObject’, receiveObject); and the as3 function receiveObject looks similar to this:
.mxml or .as
private function receiveObject( obj : Object ) : void { Alert.show(obj.foo); Alert.show(obj.zoo); }
That outputs respectively “bar” and “tar”.
Even more …
You can pass from .js to .as array of objects, but than with small change of the code you’d access the variables:
obj[0].foo obj[1].foo
assuming that there are two objects with same properties.