开发者

Refire a stopped Flex event

I have several event listeners configured to listen for a click event on a control.

I don't know if this is a correct assumption, but I believe I have one configured to listen "first". After the first listener catches the event, it will pass control to the remaining listeners (if needed).

This basically tells the user "you're trying to navigate somewhere, but you have unsaved changes, do you want to 1) save and continue 2) discard changes and continue 3) return and don't continue.

Here's the sample code for setting up the listeners

clickComponent.addEventListener(MouseEvent.CLICK, parentComponent.s开发者_如何学运维aveChanges);
clickComponent.addEventListener(MouseEvent.CLICK, continueOn);

Here's where I catch the event in parentComponent

private var unsavedEvent:Event = null;

public function saveChanges(e:Event):void {

    if (unsavedEvent == null) {

        unsavedEvent = e;
        e.stopImmediatePropagation();

        Alert.buttonWidth = 150;
        Alert.noLabel = "Discard Changes";
        Alert.yesLabel = "Save Changes";
        Alert.cancelLabel = "Return";

        var alert:Alert = Alert.show(
            "You have unsaved changes!  What would you like to do?                       ",
            "Save?",
            Alert.NO | Alert.YES | Alert.CANCEL, 
            this, 
            handleUnsavedResponse, 
            null, 
            Alert.YES);
    }
}

private function handleUnsavedResponse(evt:CloseEvent):void {

    var takeAdditionalAction:Boolean = false;

    if(evt.detail == Alert.YES) {
        save();
        takeAdditionalAction = true;
    } else if(evt.detail == Alert.NO) {
        discard();      
        takeAdditionalAction = true;
    }

    if (takeAdditionalAction) {
        dispatchEvent(unsavedEvent.clone()); // rethrow the event we caught.  not WORKING
    }

    unsavedEvent = null; //reset the saved event
}

The call for dispatchEvent(unsavedEvent.clone()); doesn't seem to work (or at least the event is not being captured by continueOn) At first I tried redispatching the event without the clone() call, but that didn't work either.

Can I do this? Is there a better way?


Got it! I needed unsavedEvent.target.dispatchEvent(unsavedEvent.clone()); instead of dispatchEvent(unsavedEvent.clone());

I'm thinking that rethrowing the event also changes the target (i.e. to parentComponent from clickComponent), which would explain why my other listener wasn't responding to the event in the original code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜