I have a datagrid w/ a custom item renderer, as follows:
<mx:AdvancedDataGridColumn dataField="file">
<mx:itemRenderer>
<fx:Component>
<mx:HBox paddingLeft="2">
<fx:Script>
<![CDATA[
import mx.core.BitmapAsset;
[Embed(source="components/download.png")]
[Bindable]
public var imgCls:Class;
public function IOErrorEventExample():void {
var loader:URLLoader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
var request:URLRequest=new URLRequest("http://www.site.com/"+data.file);
loader.load(request);
}
private function ioErrorHandler(event:IOErrorEvent):void {
if ( String(event) != null ){
// load the itemrenderer image here if the file exists on our server
var imgObj:BitmapAsset = new imgCls() as BitmapAsset;
myImage.source=imgObj;
}
else {
// don't load the itemrenderer image if the file doesn't exist yet
}}
]]>
</fx:Script>
<mx:Image id="myImage" creationComplete="IOErrorEventExample();"/>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
So, if I have the actual file on my server, I want to display download.png image …however, when I compile & run the code above, the .png image appears randomly..regardless of if “file” exists. What am I doing wrong?
You need to close off the ioErrorHandler function by adding one last curly brace before you close the CDATA tag
Can’t say for sure that will fix the problem tho, the logic looks correct. You could try adding an Event.COMPLETE event listener to really make sure the file does exist and is being loaded.
EDIT:
You can shorten down this event handler function to just this because:
1. This function is called only when an IOErrorEvent is dispatched, so there is always going to be an event object present when it is called. (The if statement isn’t needed)
2. Even if you left the if statement in, there isn’t a need to call the else statement since in the event that the file does exist, this function won’t be called.