Last night I ran into an issue I just couldn't understand with some Flex development I was doing. Let me show the code that wasn't working:
1 <mx:DataGridColumn dataField="filename_thumbnail" headerText="Thumbnail" width="250">
2 <mx:itemRenderer>
3 <mx:Component>
4 <mx:Image height="50" width="50" source="{(data.thumbnail_filename!=null)?baseurl+'/images/spotlight/'+data.thumbnail_filename:baseurl+'/images/spotlight/'+data.filename}" />
5 </mx:Component>
6 </mx:itemRenderer>
7 </mx:DataGridColumn>
This code creates an inline component to render an image in a data grid. I use some simple logic to see if the thumbnail column has a value. If it does, I want to show that image. If not, I want to show the value in the image column.
My original code had a hard coded path in it, which of course didn't work when the code was sent to production. (Duh.) So I switched to using a variable baseurl. This was a valid variable (I could Alert it inside my MXML file), but I kept getting an unknown variable error when I tried to compile it.
Luckily I was able to find help on the cfflex IRC channel. Toby Tremayne found this from the docs:
2 <mx:itemRenderer>
3 <mx:Component>
4 <mx:Image height="50" width="50" source="{(data.thumbnail_filename!=null)?baseurl+'/images/spotlight/'+data.thumbnail_filename:baseurl+'/images/spotlight/'+data.filename}" />
5 </mx:Component>
6 </mx:itemRenderer>
7 </mx:DataGridColumn>
The <mx:Component> tag defines a new scope within an MXML file, where the local scope of the item renderer or item editor is defined by the MXML code block delimited by the <mx:Component> and </mx:Component> tags. To access elements outside of the local scope of the item renderer or item editor, you prefix the element name with the outerDocument keyword. http://livedocs.adobe.com/flex/201/langref/mxml/component.htmlTurns out - this inline component acted like a whole other MXML file. A bit like how a ColdFusion custom tag would act if we could define one inline. What I don't quite get is why I can access data ok. I'm assuming Flex passes it in automatically since it recognizes I'm in a datagrid. As the docs say, it is easy enough to fix. I simply add the outerDocument keyword like so:
1 <mx:DataGridColumn dataField="filename_thumbnail" headerText="Thumbnail" width="250">
2 <mx:itemRenderer>
3 <mx:Component>
4 <mx:Image height="50" width="50" source="{(data.thumbnail_filename!=null)?outerDocument.baseurl+'/images/spotlight/'+data.thumbnail_filename:outerDocument.baseurl+'/images/spotlight/'+data.filename}" />
5 </mx:Component>
6 </mx:itemRenderer>
7 </mx:DataGridColumn>
2 <mx:itemRenderer>
3 <mx:Component>
4 <mx:Image height="50" width="50" source="{(data.thumbnail_filename!=null)?outerDocument.baseurl+'/images/spotlight/'+data.thumbnail_filename:outerDocument.baseurl+'/images/spotlight/'+data.filename}" />
5 </mx:Component>
6 </mx:itemRenderer>
7 </mx:DataGridColumn>
Comment 1 written by Ken Dunnington on 10 April 2007, at 8:37 AM
Comment 2 written by Raymond Camden on 10 April 2007, at 8:50 AM
Comment 3 written by Arul on 10 April 2007, at 11:01 AM
Comment 4 written by Rick Root on 10 April 2007, at 9:41 PM
I ended up solving it by using mx.core.application.Application.varname (or something like that)
Later, I discovered that "parentDocument" is much easier to type, as is parentApplication.
I've never seen "outerDocument" before... like all good programming languages, there are a billion ways to do things.
Comment 5 written by Elliott Sprehn on 11 April 2007, at 4:04 AM
If you're really interested then set the compiler setting to save the generated AS3 code and you can see what your MXML is actually doing.
Its a good way to understand the magic that is MXML and what mxmlc is really generating for you.
Comment 6 written by Raymond Camden on 11 April 2007, at 7:07 AM
Comment 7 written by hua on 11 April 2007, at 10:56 AM
http://www.cornilliac.com/machblog/index.cfm?event...
Comment 8 written by Raymond Camden on 11 April 2007, at 11:02 AM
[Add Comment] [Subscribe to Comments]