Passing objects with ExternalInterface from JS to Flex

submit to reddit

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.

Related posts:

  1. Tips and tricks in ExternalInterface communication!
  2. Storing JavaScript objects in html5 localStorage
  3. ExternalInterface from JavaScript to IE/Firefox

You are a GREAT developer? Click here to answer the weekly quiz!

This entry was posted in flex 3, javascript, micro tutorial and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">