Question

In order to make use of passing parameters in some event, I used custom events. I have a TabbedViewNavigatorApplication for mobiles. In which I have a couple of views. After a period of time 1 of the views pops up a window (App rating) which has 3 options (1. rate now, 2. remind later, 3. Do not remind).

I created a custom event CustomMouseEvent (there is a nice site here for generating custom event) which can take in an extra parameter of Object type. I have a component for this popup window RatePrompt which has click handlers for the 3 options for rating. These click handlers will in turn build parameters and dispatches this custom event to another view which can handle this dispatched event, but somehow the event is not able to reach the second dispatch ratingPromptHandler.

In HomeView I use

<fx:Declarations>
    <components:RatePrompt id="rateMessage"/>
</fx:Declarations>

RatePrompt component

<fx:Script>
    <![CDATA[
        .
        .
        .
        protected function okClick(event:MouseEvent):void
        {trace("OK");
            var params:Object = new Object();
            params[Tables.RATE_REMINDER]=Tables.YES;
            var eventToBeDispatched:CustomMouseEvent = new CustomMouseEvent(CustomMouseEvent.CLICKED,params);
            dispatchEvent(eventToBeDispatched);
            closeMe(event);
        }

        protected function remindClick(event:MouseEvent):void
        {trace("remind");
            var params:Object = new Object();
            params[Tables.RATE_REMINDER]=Tables.NO;
            var eventToBeDispatched:Event = new CustomMouseEvent(CustomMouseEvent.CLICKED,params);
            dispatchEvent(eventToBeDispatched);
            closeMe(event);
        }

        protected function neverClick(event:MouseEvent):void
        {trace("never");
            var params:Object = new Object();
            params[Tables.RATE_REMINDER]=Tables.NEVER;
            var eventToBeDispatched:Event = new CustomMouseEvent(CustomMouseEvent.CLICKED,params);
            dispatchEvent(eventToBeDispatched);
            closeMe(event);
        }
    ]]>
</fx:Script>
.
.
<s:Button id="btnOK" label="Ok" width="50" height="30" styleName="stylableButton" click="okClick(event)"/>
<s:Button id="btnRemind" label="Not now" width="85" height="30" styleName="stylableButton" click="remindClick(event)"/>
<s:Button id="btnNever" label="Never" width="85" height="30" styleName="stylableButton" click="neverClick(event)"/>
.
.

Custom event class

import flash.events.Event;
public class CustomMouseEvent extends Event
{
    public static const CLICKED:String = "clicked";     
    private var _parameter:Object;      
    public function CustomMouseEvent(type:String, parameter:Object, bubbles:Boolean = false, cancelable:Boolean = false):void   { 
        super(type, bubbles, cancelable);
        _parameter = parameter;
    }

    public function get parameter():Object      {
        return _parameter;
    }

    override public function clone():Event      { 
        return new CustomMouseEvent(type, _parameter, bubbles, cancelable);
    } 

    override public function toString():String      { 
        return formatToString("CustomMouseEvent", "type", "parameter", "bubbles", "cancelable", "eventPhase"); 
    }
}

And in a view I open popup and add event listeners and handlers

public function popupRateMessage():void
{
    PopUpManager.addPopUp(homeView.rateMessage, homeView.homeContainer as DisplayObjectContainer, false);
    PopUpManager.centerPopUp(homeView.rateMessage);
    homeView.rateMessage.y=this.homeView.stage.fullScreenHeight/2-(homeView.rateMessage.height/2);
    homeView.rateMessage.message = Constants.RATE_REMINDER_PROMPT;
    trace("AAA");
    homeView.rateMessage.btnOK.addEventListener(CustomMouseEvent.CLICKED, ratingPromptHandler);
    homeView.rateMessage.btnNever.addEventListener(CustomMouseEvent.CLICKED, ratingPromptHandler);
    homeView.rateMessage.btnRemind.addEventListener(CustomMouseEvent.CLICKED, ratingPromptHandler);
}

private function ratingPromptHandler(event:CustomMouseEvent):void {
    trace(event.parameter);
    trace(event.parameter[Tables.RATE_REMINDER]);
}

During debug I can only see the below trace results when btnOK is pressed. Event is not able to reach ratingPromptHandler handler.

AAA
OK

Please help me in pointing ways I can fix this. Thanks

Was it helpful?

Solution

I think your problem is here: dispatchEvent(eventToBeDispatched);

This should be dispatched by the button you have clicked on. The way you are using it, it will get dispatched by the root component of your application.

You should use something like: event.target.dispatchEvent(eventToBeDispatched);

Just a note. Why are you dispatching another event after click? Just call another method with whatever parameters you need in a desired class like: CLASS.ratingPromptHandler(params);

Hope I could help.

OTHER TIPS

Have you added the following to your mxml file?

   <fx:Metadata>
            [Event(name="clicked", type="CustomMouseEvent")]
   </fx:Metadata>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top