Reputation: 2945
Background:
We have several flash objects written in FLEX and being rendered inside a single HTML page.
We are using javascript to communicate between the objects and perform other page related operations.
Execution Flow:
In the creationComplete event of the FLEX code of our objects we execute the following (pseudo-ish) code:
if (ExternalInterface.available) {
ExternalInterface.addCallBack("initialize");
ExternalInterface.callMethod("ready");
} else {
Alert.show("No External Interface Available!");
}
Which executes the following javascript (psuedo-ish) code:
Object.prototype.ready = function(){
//this is simplified. More happens but all we care about is the following
flexObject.initialize();
}
Which executes the follwoing flex (psuedo-ish) code:
public function initialize() {
var asyncObject:Object = remoteService.executeMethod();
asyncObject.addResponder(function(object:Object){
if (ExternalInterface.available) {
ExternalInterface.callMethod("responseMethod");
} else {
Alert.show("ExternalInterface dissapeared!?");
}
});
}
Which SHOULD execute the following javascript (psuedo-ish) code:
Object.prototype.responseMethod = function() {
alert("responded!");
}
Problem:
Sometimes the last javascript function outlined is never called. When this happens none of the error alerts appear, the final code is just not executed but everything up to this point has executed successfully.
This happens about 1/100+ times in Chrome/Firefox This happens about 1/10 times in IE8 This happens about 9/10 times in IE7 (full disclosure - the only machine we have to use for testing this is also much slower)
Since the problem is intermittent and becomes more apparent in slower browsers/machines my suspicion is that it is timing related.
If I stop the first flex 'intiailize' call from happening until I click a button in the UI instead of automatically on what is essentially 'creationComplete' everything ALWAYS works.
I may be able to solve the problem simply by introducing a 'delay' in the javascript but I really really don't like that hack.
Upvotes: 1
Views: 689
Reputation: 18193
I think @wvxvw's comment is right on the money.
In your initialize() method, it's very likely that the asyncObject variable is getting garbage collected (when the problem happens). Thus you see no errors at all.
Declare the asyncObject variable at the class level (not inside the initialize() function), and that will probably fix the issue:
var asyncObject:Object;
public function initialize()
{
asyncObject = remoteService.executeMethod();
....
}
You also might want to look at using an HTTPService (or the other flex service classes) which returns an AsyncToken and gives you better handling of responses if your code makes many simultaneous requests....
Upvotes: 1