Showing posts with label flex. Show all posts
Showing posts with label flex. Show all posts

Friday, January 15, 2010

Flex 3 ProgressBar in polled mode stops working when it reaches 100%

The ProgressBar component in Flex 3 (I am using SDK 3.4.1.10084 ) has 3 modes of operation: event, polled and manual. For a project I chose to use the polled mode to display the upload progress of multiple files using two ProgressBar components: one for individual files, and one for the overall progress.

The problem was that while the overall progress bar worked correctly, the individual file progress bar completely stopped working when the first file finished uploading. I checked the numbers, and they were fine, so the problem had to lie with the component.

The explanation is that the timer used to update the progress bar is stopped once the bytesLoaded property equals bytesTotal. Adobe will most surely call this a feature, as it saves resources for most use cases, when the progress bar is used to track a single operation at a time. However, in my case it turned out to be a bug, since I needed to track multiple operations. Had I been aware of it, I would probably have engineered my code to use the event or manual mode. But Adobe have either not documented this, or have hidden it very cleverly.

The solution in my case was either to prevent the timer from stopping or to restart it after the progress reached 100%. I did not want to keep it going indefinitely, as that would have cause the kind of resource leak which Adobe tried to prevent. So I looked in the ProgressBar class for instances where the timer was restarted, and it seemed that the best way to force a restart would be to toggle the mode property itself when a file finished uploading:

this.currentFileProgressBar.mode = ProgressBarMode.MANUAL;
this.currentFileProgressBar.mode = ProgressBarMode.POLLED;

There would have been other alternatives, of course, but this worked best in my project.
One of the alternatives is to use the BetterProgressBar component in the CleverPlatypus framework, which simply adds a value setter to the progress bar, as you would expect it to have anyway.

Thursday, September 10, 2009

Javascript injection from Flex into the HTML wrapper - problems with IE, of course

Here are some tips which might save someone some headaches (which you inevitably get when you start working with Internet Exploder).

First and foremost, if you want to communicate from Flex to (regular) Javascript using ExternalInterface in Internet Exploder (I was testing on version 6), you had better put an id attribute on the [object] and [embed] elements, or else it will either silently fail, or give you the wonderfully descriptive and logical error message 'null' is null or not an object. If I didn't know any better, I'd say it's rather philosophical. Oh, and it's always at character 118, line 1. I dare you to find it :)
You find this information if you read the first note on the Adobe documentation page for ExternalInterface.

When I said 'regular' earlier, I meant javascript that's already embedded in the page. Now, if you want to do nifty stuff like Javascript injection, you would probably use Abdul Qabiz's fantastic component. The reason I needed it was that our Flex application needed to be completely independent of the html wrapper it happened to find itself in, but also needed to open IFrames and move them around and do all kinds of nifty stuff (you can tell I like this word). That meant that the script would have to be embedded at runtime in the page by Flash.

Sounds doable, right? In Firefox, yes. In IE, haha, you've got another thing coming. IE and 'simplicity' have proven to be at opposite ends of the spectrum, time and time again.

I won't drone on about the specifics too much. But here are some major points to keep in mind:
For me the workaround didn't work yet... I'm trying to tweak it somehow to get it going. And it's great how you feel completely in the dark when it comes to these things, as the documentation seems to be lacking. It would have been great if IE6 had had the public bug tracking application IE7 and 8 do. But it would have probably crashed very early on, due to overuse.