RKCY
RKCY

Reputation: 4135

click on image to dispatch event in flex

I have a image in renderer class and if i click image i need to dispatch a event to the main mxml page in flex.

In Renderer Class:-

public function clickOnMoreImage(event:Event):void {                
    var moreImageEvent:Event = new Event("clickOnMoreImage");
    var parent:EventDispatcher = parentDocument.parentDocument as EventDispatcher;
    if (parent != null) {
        parent.dispatchEvent(moreImageEvent);
    }
}


<mx:Image id="imgStatus" buttonMode="true" useHandCursor="true" click="clickOnMoreImage(event)" />

parent class:-

this.addEventListener("clickOnMoreImage", showMoreImagePopUpData);

public function showMoreImagePopUpData(event:Event):void {
    Alert.show("clicked on More Image");
}

This is not working. can i know any work around for this?

Upvotes: 1

Views: 1419

Answers (1)

Timofei Davydik
Timofei Davydik

Reputation: 7303

  1. Don't use parent or parentDocument in this case. You cannot be sure of nesting level of your renderer against top application.
  2. The "Flex" way is to use event bubbling.

Dispatch event by renderer itself (with bubbles set to true). It will "bubble" (in case the renderer is not a child of a popup) up to stage, so you can listen to it in any parent of the renderer (e.g. -> DataGroup -> List -> Application -> SystemManager -> Stage).

var moreImageEvent:Event = new Event("clickOnMoreImage", true); //2nd parameter "true"
dispatchEvent(moreImageEvent);

Upvotes: 3

Related Questions