tags:

views:

608

answers:

3

Hi, I'm developing a flex application with 4 tabs. When the user switches a tab I want to reset the previous tab to its initial state. Also I need to alert the user, if he hasn't saved the changes he made if any, will be lost.

I'm planning to set a variable in the Model, and set/reset it if any change happens in a field under a tab. But how do I monitor this? Is there any listener available for this?

Also how do I check and reset the state of the previous tab? The contents that come under the tab is from components only.

[EDIT] My questions are:

  1. How do I check if the user has made any edits in the current tab? Some fields are generated dynamically too.
  2. I'm calling a function in the onchange event of TabNavigator and asks the user if he really want to switch the tab.I want the other tab to load its contents only if the user clicks Yes to the Alert box I'm popping up. But now the confirmation box pops up, and the contents are loaded into the other tab and if the user clicks No it goes back to the other tab. How do I prevent the action of loading the contents of the other tab at all till the user presses Yes?

Please provide your valuable inputs.

+1  A: 

For 1) you could dispatch an Event every time a user edits a field. The event can be handled by a command which will update some properties in your model with the right info about what got updated. Then whoever cares in your view can bind to those properties.

For 2) in the onChange() handler call event.preventDefault(). Then you can programatically select the next tab only if the user clicks Yes.

Steve

Stephen Horvath
+2  A: 

Answer to question 1 is as follows; Use a Boolean variable to track if a user has edited data. When the user selects a tab set this variable to false. Listen for the change event on all fields within the tab. Set the change event handler for all fields to be a method which sets the Boolean to true. For the dynamic fields, add the same change event handler that the other fields have. Do this as soon as you create each dynamic field. See the code below;

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
    <mx:Script>
     <![CDATA[
      private var userChangedData:Boolean=false

      function onUserChangedData()
      {
       trace("onUserChangedData")
       userChangedData=true
      }

      function onTabChanged()
      {
       trace("ontabchanged")
       trace(userChangedData)
       userChangedData=false
      }
     ]]>
    </mx:Script>
    <mx:Panel title="TabNavigator Container Example"
        height="90%"
        width="90%"
        paddingTop="10"
        paddingLeft="10"
        paddingRight="10"
        paddingBottom="10">

     <mx:TabNavigator id="tn"
          width="100%"
          height="100%"
          change="onTabChanged()">
      <!-- Define each panel using a VBox container. -->

      <mx:VBox label="Panel 1">
       <mx:Label text="TabNavigator container panel 1"/>
       <mx:TextInput text="default"
            change="onUserChangedData()"/>
       <mx:CheckBox label="check something"
           change="onUserChangedData()"/>
      </mx:VBox>

      <mx:VBox label="Panel 2">
       <mx:Label text="TabNavigator container panel 2"/>
      </mx:VBox>

      <mx:VBox label="Panel 3">
       <mx:Label text="TabNavigator container panel 3"/>
      </mx:VBox>
     </mx:TabNavigator>


    </mx:Panel>

Phil C
Is it possible to add a global onchange/onkeypress method that hooks to the complete application and sets the boolean? Otherwise I'll have to edit at multiple places to add the onchange event.
whoopy_whale
Thanks for the solution. But setting a handler for onchange event will not work out well for TabNavigator.I found a solution here at http://natescodevault.com/?p=43 and it works well too...
whoopy_whale
Interesting. Didn't realise that about the change event for the tab navigator
Phil C
A: 

I don't have the reputation to add comments yet, but to answer your question:

"Is it possible to add a global onchange/onkeypress method that hooks to the complete application and sets the boolean? Otherwise I'll have to edit at multiple places to add the onchange event. – Basani"

Yes, have each place that needs to signal that "something changed" dispatch an Event. Then have a Command watching for dispatches of that event. That Command can do all the processing you need, including setting the userDataChanged boolean in the model.

It sounds like you're using Cairngorm based on how you tagged the question, so this should be easily supported.

Stephen Horvath