Using preventDefault for a View

413 Views Asked by At

I've created a simple TabbedViewNavigatorApplication. Two of the tabs are going to send text and email, respectively, rather than load a new View. So I need to prevent the default behavior of pushing the new View.

Documentation says a view can cancel a navigation operation by canceling its FlexEvent.REMOVING. However, there apparently is no such Constant for FlexEvent. There is, however, for ViewNavigatorEvent, as described farther down on that first link.

So here's what I've got, but I neither see the trace statements nor am able to stop the navigation:

<?xml version="1.0" encoding="utf-8"?>
<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" creationComplete="completeHandler(event)">
<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;

        import spark.events.ViewNavigatorEvent;

        private function completeHandler(e:Event):void
        {
            vwText.addEventListener(ViewNavigatorEvent.REMOVING, removingHandler);
        }

        private function removingHandler(e:ViewNavigatorEvent):void
        {
            trace("removingHandler::");
            e.preventDefault();
        }

        private function doSomething(e:MouseEvent):void
        {
            trace("Do Something");
        }

    ]]>
</fx:Script>
<s:ViewNavigator  id="vwText" label="Text" width="100%" height="100%" click="doSomething(event)" firstView="views.SendasTextView"/>
<s:ViewNavigator  label="Email" width="100%" height="100%"  firstView="views.SendasEmailView"/>
<s:ViewNavigator label="History" width="100%" height="100%" firstView="views.HistoryView"/>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

1

There are 1 best solutions below

1
On

You need to take

vwText.addEventListener(ViewNavigatorEvent.REMOVING, doSomething);

out of your script block.

It needs to be inside of a function. You can use the creationComplete event to create an init function that will add that listener.