I want to call a method in flex from javascript side,
so that I can retrieve javascript object which contains data in flex.
now I'm trying like
var result:Object = new Object()
var keyset:Array = data.getKeySet();
for (var i:int = 0 ; i < keyset.length ; i++) {
result[keyset[i]] = data.get(keyset[i]);
}
return result;
but it do not work. how can I make it right?
p.s. I know it is fundamental question, but I couldn't find anything even though I googled for an hour. So please help!
To communicate between Flash/Flex and JS on the page, use the ExternalInterface class. You can't directly pass objects, but you can convert your object into a serialisable/string. Here's how you'd call a function called 'myFunc' and set it two arguments, a string and a number:
ExternalInterface.call('myFunc',1,'aString');
After the function name, which must always be a string, there is a ...rest parameter. Simply enough, this means you can send any number of arguments to the function, separating them with commas (we do two here).
If you used AS2 at any point in the past you may know the 'eval' function, this was inherited from (and is thus still used by) JS - it analyses a string and attempts to parse it into JavaScript, using this you can literally send Javascript code instead of a func/args:
ExternalInterface.call('alert("Hello!")');
If you want two-way communication, use the ExternalInterface.addCallBack function to register a function as callable from JS.
In case of errors when doing this, you may need to adjust your embed code: "In the object tag for the SWF file in the containing HTML page, set the following parameter:
param name="allowScriptAccess" value="always"
I believe you cannot call a method in AS3 from JS directly (and vice-versa). There should be an interface for it though, where one can call the other. If i remember correctly, you should use the ExternalInterface API.
Also, you can't pass Flex objects to JS (and vice-versa) also. Try building a generic object that is serializable to JSON and use that serialized data in passing data to each other. The receiving party can parse it back to use the data. In this example, the code passed a string from JS to AS3.
In your case, the Flex function would:
build an object
stuff it with data
serialize it into a JSON string
return the string to the caller
Then when JS calls the function:
JS receives the string
Use JSON.parse() to reconstruct the JSON string into a JS object
use the object
Related
I am having an issue whereby I create a RegExp object in a content script and pass it as part of an object back to the main script using self.port.emit().
Somewhere along the way it seems to lose its identity as a RegExp and also its toString abilities. The following returns false in the main script, but true in the content script:
Object.prototype.toString.call(regexp) == '[object RegExp]';
regexp instanceof RegExp;
Interestingly for Arrays passed in the same way the following is true:
Object.prototype.toString.call(array) == '[object Array]';
Am I missing something?
The Add-on SDK doesn't pass objects around when you send messages, only strings - it essentially calls JSON.stringify() on one side and then JSON.parse() on the other. The result is easy to predict:
console.log(JSON.stringify(new RegExp()));
This gives you "{}". In other words, JSON.stringify() treats "custom" objects as normal objects without any properties, object prototypes and such are ignored. What you get in your main code is a plain object, same as if you call new Object().
If you need to pass a regular expression to your main code - send regexp.source, create an actual regular expression on the other side. Sending actual objects around isn't possible.
What about if you just sent the regex pattern instead of the whole rexexp object? e.g. encodeURIComponent(regexp.source);
I'm working on a facebook app with JS SDK...
I want to create a function that makes an API call to the Graph API:
This is what I got:
function a(reference){
FB.api('/me', function(respond){
var it = respond.reference[0].name;
alert(it);
});
}
I try executing it this way but it doesn't work.
a("inspirational_people");
So you’re feeding the text string "inspirational_people" into your function, and you want to access a property of the response object having that very name?
That’s quite easy, basic JavaScript syntax: Every property that you can access via object.propertyName you can also access as object["propertyName"].
So in your case, since you want to access response.inspirational_people[0].name, it’s response[reference][0].name – the actual string value of reference is evaluated at runtime, and you should end up having what you wanted.
I'm stuck on a QtWebKit bridge issue on Windows and I ran out of
options. I don't have access to the exact code used but its something like below.
C++
class MyObject : QWidget {
Q_OBJECT
public:
QString data() const;
void setData(QString);
Q_PROPERTY(QString data READ data WRITE setData)
};
/* ... */
MyObject myObject;
frame->addToJavaScriptWindowObject("myObject", &myObject);
JavaScript
function drop(e) {
var url = e.dataTransfer.getData('url');
//alert(url); // Displays url correctly
myObject.data = url; // Assigns url to C++ object myObject
}
The alert box correctly displays the string value, e.g. 10.10.0.1.
The parameter on setData gives me the string "1". If I then view then memory at that address, I see the full url in memory (formatted as UTF-32 (4 bytes per character)), but whatever I try (toStdString, toAscii, utf16 - just to get sensible data) I do not seem to be able to get/use the whole string.
I event thought that maybe the debugger is playing a trick on me, so I pass the data to the method that actually needs this data (which also used a QString) it all might work - but sadly no.
Even if I make MyObject::setData Q_INVOKABLE and call setData directly I get the same
behaviour:
myObject.setData(url); // Assigns url to C++ object myObject
If I just pass the data as literal, all DOES work correctly, like
myObject.setData('10.10.0.1');
or
myObject.data = '10.10.0.1';
I do not understand why passing a literal works but a variable not, the 'url' variable should be a string type.
I'm partly there. String passing works as expected. However, the C++ object is embedded on a webpage and wraps an ActiveX (ActiveQt) object.
For this, I've seen some pages where you create a class that inherits from a QWebPage. There you have a createPlugins method that asks the QUiLoader to construct the widget for you (alternatively you use the Qt Metatype system). And my C++ object is registered.
When I use this custom WebPage on my WebView (using setPage), the strings are passed in wrongly. When I disable calling setPage, the strings passed correctly.
So string passing works (in some conditions).
I've created a new issue for this: Qt Webkit bridge ActiveQt string
When I use the System.Web.Script.Serialization.JavaScriptSerializer.Serialize method, I get back valid JSON code.
This is usually perfect, but sometimes I want to get back the result as a Javascript object, not JSON. The Serialize method has an overload that takes a SerializationFormat parameter. That looks perfect... but it is marked as internal!
How can I get out a string of Javascript from the Serializer?
Take a look at the JScript DLL Eval object's JScriptEvaluate method (http://msdn.microsoft.com/en-us/library/microsoft.jscript.eval.jscriptevaluate.aspx):
using Microsoft.JScript;
var MyJSObject = Eval.JScriptEvaluate("{a:'Property1',b:'Property2'}", Engine);
To my knowledge, the NPAPI plugin methods return only the NPVariant *result.
But i need a multiple strings and integers that are passed to the plugin method as parameters, modified inside the plugin method and then their modified value is used in the javascript that is calling this plugin method.
Can i get some help regarding this matter?
Is it the case that only the value that is returned by the method can be used in javascript or we just need the variable type to be NPVariant for it to be usable in the javascript without the plugin method returning it.
in js, you return multiple value by array or object (afaik, they are pretty much the same.) and I guess you just need the same technique for npapi plugin, i.e. create a object, do what ever you want (add what ever field you need in it), and return it.
I think you can also use the similar way to simulate the fallowing js code
function (obj) {
obj.n = 2;
obj.strs[0] = '';
obj.strs[1] = 'a';
}
(sure you need to check the type of obj etc.)
(basically my point is you just need to simulate what you can do in js~~)
(haven't try myself, tell me if I am wrong)