burdiuz / jsinterface

Automatically exported from code.google.com/p/jsinterface
0 stars 0 forks source link

Question on passing in javascript objects by reference #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.
Can I pass in a javascript object into actionscript ala java and 
liveconnect? I would like to do something like

//in javascript
actionscript.externalinterfacefunc(new jsobject('params1'));

and in actionscript it actually converts into a JSDynamic or a similar 
object. The reason I'm asking this because I need to execute a function 
inside the javascript object and from what I understand it is currently 
not possible.

Thanks!

Original issue reported on code.google.com by john...@gmail.com on 18 Sep 2009 at 8:41

GoogleCodeExporter commented 9 years ago
If you register callback via ExternalInterface.addCallback, JSInterface will 
not do 
anything with transfered data - parameters will be converted accordingly to 
ExternalInterface rules(into simple ActionScript Object without connection to 
original). You can do one from:

1. Pass to JavaScript environment object which method will be called.
[actionscript]
JSInterface.window.asObject = myObject;
[/actionscript]
In JavaScript you will get instance of FLObject instead of ActionScript object. 
FLObject instance has methods to connect with original ActionScript object 
properties and methods. So you can call method "call" with method name and 
argument 
list.
[javascript]
window.asObject.call("actionScriptMethod", [argument1, argument2, argument3]);
[/javascript]
And after that your ActionScript object will receive instance of JSDynamic 
which 
connected to original JavaScript object.

2. Create ActionScript collection object and use it instead of original 
ActionScript 
object. You can pass this collection into JavaScript after application 
initialized 
and add or call methods any time you want. Instead of creating pecial class for 
methods collection you can use simple ActionScript Object, but need firstly 
wrap it 
into JSComplex(if you will not do this, Object will be coverted into JavaScript 
object and will loose connection to original object).
[actionscript]
var myCollection:Object = {};
JSInterface.window.asObject = new JSComplex(myCollection); // For custom 
classes you 
not need wrap into JSComplex
myCollection.myActionScriptMethod = myObject.myMethod;
[/actionscript]
After that you can continue like in "1".

3. Enable JavaScript to ActionScript access and get any objects you need from 
JavaScript directly.
You an init JSIterface with this option turned ON:
[actionscript]
JSInterface.initialize(this.stage, true);
[/actionscript]
Or you can turn ON or OFF this option at any time:
[actionscript]
JSInterface.allowJavaScriptAccess = true;
[/actionscript]
You can get instance of ActionScript object from core object:
[javascript]
// FLObject.root() - this is root DisplayObject, Main object in your application
FLObject.root().get("myAsObject").call("myActionScriptMethod", [argument1, 
argument2, argument3]);
[/javascript]
Or get singleton instance or any object accessible through static 
property/method:
[javascript]
var asObject = FLObject.instance(”com.package.SomeClass.property”);
[/javascript]
And use its methods.

4. You can pass any ActionScript method into JavaScript and call it directly 
from 
JavaScript environment
[actionscript]
JSInterface.window.myActionScriptMethod = myObject.myMethod;
 [/actionscript]
And use it in JavaScript as usual:
 [javascript]
window.myActionScriptMethod(argument1, argument2, argument3);
 [/javascript]
All objects in arguments will be wrapped in JSDynamic and passed to original 
ActionScript method.

JSDynamic wrapper allows you to live-connect with original JavaSript object, so 
if 
you call method in JSDynamic instance this call will be passed to original 
JavaScript method, the same with properties.  And the same with FLObject 
instances 
in JavaScript – any call/set/get operation will be passed to original 
ActionScript 
object and its property or method(only difference in syntax – this is 
disaster for 
FLObject, because JavaScript has not Proxy like functionality).
You can closely look into FLObject documentation, I have created fake class to 
use 
asdoc with javascript object:
http://code.google.com/p/jsinterface/source/browse/trunk/Original%20files/
jsdoc_fake_source/FLObject.as
Or just download documentation.

Hope this helps.

Original comment by burd...@gmail.com on 18 Sep 2009 at 9:39

GoogleCodeExporter commented 9 years ago
With JSInterface you can pass any ActionScript object into JavaScript 
environment 
and vise versa and it will be live-connected. To force live-connection, look 
aw.external.jsinterface.JSComplex documentation. To force complex object passed 
like 
simple - look into aw.external.jsinterface.JSSimple and FLSimple(JavaScript 
object) 
documentation.

Original comment by burd...@gmail.com on 18 Sep 2009 at 9:46

GoogleCodeExporter commented 9 years ago

Original comment by burd...@gmail.com on 21 Sep 2009 at 2:07

GoogleCodeExporter commented 9 years ago

Original comment by burd...@gmail.com on 21 Sep 2009 at 2:08

GoogleCodeExporter commented 9 years ago
Thank you for your help.. I'm trying out method 4 as you suggested above but 
I'm 
hitting a javascript error.

Attached are the source files for a simple demonstration. I'm setting a method 
in 
flex to be called by js. In JS i am able to see the method (via alert) but I'm 
getting the error m, which is c._jsi.main is undefined.

I wonder if this example will be of any help to you and if you can point out 
any 
mistakes that I may be making regarding the use of your library. Thank you.

Original comment by john...@gmail.com on 22 Sep 2009 at 3:10

Attachments:

GoogleCodeExporter commented 9 years ago
Hi!
You pointed me to bug of working with SWFObject. Now it is fixed(2.4.3 
version). You 
can update JSInterface library and it will work.
In attachment Flash Builder project with working test.

Original comment by burd...@gmail.com on 13 Nov 2009 at 11:01

Attachments: