Frage

I am hoping someone can help me. I have the following function in FlashBuilder 4 using AS3:

protected function doIt():void
    {
        if (ExternalInterface.available) {
            var retData:String;
            retData = ExternalInterface.call("returndata");
            if(retData != null) {
                eidLbl.text = retData.toString();
            } else {
                eidLbl.text = "Returned Null";
            }
        } else {
            eidLbl.text = "External Interface not available";
        }                
    }

and the following javascript function it calls:

var fql_query = "some query format";
function returndata(){
    return fql_query;
}

When I run these from my site everything works fine! The flash file calls the javascript function and returns "some query format" into the proper text field.

However if I change my flash function to be as follows:

protected function doIt():void
{
    if (ExternalInterface.available) {
        var retData:String;
        retData = ExternalInterface.call("runFQL",uidLbl.text);
        if(retData != null) {
            eidLbl.text = retData.toString();
        } else {
            eidLbl.text = "Returned Null";
        }
    } else {
        eidLbl.text = "External Interface not available";
    }                
}

and the following javascript function:

function runFQL(id){
    var user_id = id;
    var page_id = "**********"; // the page id that is actually there is valid
    var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;

    FB.api({
        method: 'fql.query',
        query: fql_query
    },
    function(response){
        if (response[0]) {
                var uid = response[0].uid;
                window.alert(uid); //100001632043058
                return uid;
        } else {
                window.alert("return 1000");
        }
    });    
};

My problem is that when I run this one, it passes the id in, runs the query, and accurately pulls back the uid and displays it in the alert BUT (AND HERE IS MY PROBLEM) it won't pass it back to flash in the return. It comes back as null or undefined each time.

Can anyone tell me what I am missing. I have been scouring google for hours.

War es hilfreich?

Lösung

function(response){
    if (response[0]) {
        var uid = response[0].uid;
        window.alert(uid); //100001632043058
        return uid;
    } else {
        window.alert("return 1000");
    }
});

Is called when the FB.api function completes not when you call runFQL. So runFQL runs through and returns undefined everytime.

What you can do is once the FB.api call completes call a function inside the flash app and pass the return value there. So basically do a ExternalInterface.addCallback in flash and call that function from javascript.

function runFQL(id){
    var user_id = id;
    var page_id = "**********"; // the page id that is actually there is valid
    var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;

    FB.api({
        method: 'fql.query',
        query: fql_query
    },
    function(response){
        if (response[0]) {
                var uid = response[0].uid;
                window.alert(uid); //100001632043058
                // needs ExternalInterface.addCallback("sendDataBack",handleData); in your 
                // as3 code
                yourSwf.sendDataBack(uid); 
        } else {
                window.alert("return 1000");
        }
    });    
};

Andere Tipps

You have a very small work flow issue. In your JavaScript code you are calling FB api and then the function ends, in which case it will return a undefined since nothing is returned. But you are thinking that the nameless function is what does the return when actually it isn't even called yet because the API didn't even get a response. What you need to do is change the following line of code.

return uid;

to

myHTMLFlashObject.someFunctionName(uid);

and in your flash project you need to add a function someFunctionName and add it as an ExternalInterface callback function.
Remember APIs are not asynchronous so you can expect the function runFQL to return undefined long before the API as sent a response.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top