views:

55

answers:

3

I would like to ask if there is a liveconnect equivalent for ActionScript 3. I understand that there is the ExternalInterface class inside AS3 but it only supports calling a method by name. The really cool thing about Java and LiveConnect is that you can do something like

function jsFunc(name) = {
  this.name = name;
  this.talk = function(){
    alert('hello world my name is ' + this.name);
  }
}

javaapplet.function(new jsFunc("bob"));

The above approaches pseudo code since I never tested it but I've seen it in action. In AS3, while I am able to pass in an instance of JavaScript "object" into AS, it is often converted into an ActionScript Object instance which does away with all the functions as far as I'm aware.

I saw an implementation of JSInterface but I don't think it does specifically that. Is there any way to make OO like javascript work with ActionScript 3?

A: 
ExternalInterface.call("f = function() { alert('Is this like live connect?'); }");
radekg
The code will violate the Flash security sandbox. (Error #2060)
Andy Li
What version of Flash Player?
radekg
It only violates it if you didn't set the correct parameters when embedding the flash object.
davr
A: 

Actually the main usage scenario is to have JS "objects" interacting with the Flex SWF application. Therefore when the JS "object" wants to say wait for something happening in the SWF object, it will put in a "this" with a callback.

After researching, the way I used to accomplish this is via the Flex Ajax bridge. It may not be a direct answer to the way I phrased the question but it was sufficient for my needs.

Basically what I do is via FABridge, after initializing, I'll attach event listeners to the object.

// JS
FlexApp.addEventListeners('flexDidSomething', this.doSomething().bind(this)); //using mootools;

and in Flex, the main application itself

// AS
dispatchEvent(new CustomCreatedEvent(param1, param2));

And inside the JS function I'll access the get methods of the event object to retrieve the params.

There's tight coupling in that sense but it works at least for what I need.

Hope this is helpful!

johncch
A: 

JSInterface designed exactly for such things.

a_w
reply, yes it is. I found a show stopping bug and filed it though and switched to FABridge (for the cool init callback) and using a router to route back requests.The author of JSInterface says that the bug is fixed in the newest point release.
johncch