Call parent function from an itemRenderer
I want to call the parent function called "edit_groups()" from the itemRenderer. The code for my itemRenderer is:
<mx:VBox id="vbx_container" paddingBottom="4" paddingLeft="4" paddingRight="4" paddingTop="4" borderStyle="solid"
dropShadowEnabled="true" width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Canvas width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Image id="image" width="100" height="100" source="{data.thumb}" scaleContent="true" maintai开发者_StackOverflow社区nAspectRatio="true"
complete="{image_smoothing_handler(event);}" trustContent="true" doubleClick="{CALL THE PARENT FUNCTION "edit_groups()"}"/>
</mx:Canvas>
</mx:VBox>
And I call my itemRenderer from an application like:
list_groups_modify.itemRenderer=new ClassFactory(groups.list_groups_modify_item_renderer);
<mx:Label text="{data.label}" textAlign="center" maxWidth="60" toolTip="{data.label}"/>
Regards Zeeshan
Try this, using parentDocument
:
<mx:VBox id="vbx_container" paddingBottom="4" paddingLeft="4" paddingRight="4" paddingTop="4" borderStyle="solid"
dropShadowEnabled="true" width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Canvas width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Image id="image" width="100" height="100" source="{data.thumb}" scaleContent="true" maintainAspectRatio="true"
complete="{image_smoothing_handler(event);}" trustContent="true" doubleClick="parentDocument.edit_groups()"/>
</mx:Canvas>
</mx:VBox>
You can reference the outerDocument
like this
<mx:VBox id="vbx_container" paddingBottom="4" paddingLeft="4" paddingRight="4" paddingTop="4" borderStyle="solid"
dropShadowEnabled="true" width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Canvas width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Image id="image" width="100" height="100" source="{data.thumb}" scaleContent="true" maintainAspectRatio="true"
complete="{image_smoothing_handler(event);}" trustContent="true" doubleClick="{outerDocument.edit_groups()}"/>
</mx:Canvas>
</mx:VBox>
Make sure what you are trying to reference is set to a public function or variable.
If you are getting error 1069 and your item renderer is a separate mxml component you may need to use:
parentDocument.parentDocument.functionName();
I have also faced similar situations, but according to me the best way is to dispatch some custom event from the item renderer and catch that event inside the parent component. Inside the eventlistener call the parent's function. This will work even if the itemrenderer is in separate mxml file or actionscript class.
I stumbled across this post having a similar problem following an upgrade to Flex SDK 4.6. I was getting the dreaded error 1069 with code that used to work fine. The preferred answer did not work.
However, I solved it by changing it to use:
document.owner.parentDocument
You can also use this piece of code (placed in the item renderer) to find the next applicable parent:
internal function findTarget():MyTargetClass
{
for (var p:* = this; !(p is MyTargetClass || p == null); p = p.parentDocument) {}
return p;
}
Usage example:
override public function set data(value:Object):void
{
super.data = value;
findTarget().myFunction(value);
}
精彩评论