7 Replies Latest reply on Oct 31, 2006 12:29 PM by ntsiii

    run function everytime a component is viewd / selected

    ljonny18 Level 1
      hi - can anyone tell me how to run a function within a component evey tiome the component is selected / viewed - (not created)

      i.e. I dont want the function to run in the creationComplete handler as it is needed to be ran as soon as (and everytime) the component is selected / viewed - whi may be multiple times during the runtime of the application etc....

      is there some sprt of event I can use....?

      thanks
      Jon.
        • 2. Re: run function everytime a component is viewd / selected
          ljonny18 Level 1
          no joy with the focusIn event :(

          my component is embedded within a vBox and every time the component is seleted / viewed / openend etc... (Not just created!) I want to run a function within this component....

          thanks,
          Jon.
          • 3. Re: run function everytime a component is viewd / selected
            inlineblue Level 1
            Well, this all depends on the exact nature of your component and what it means to be selected, viewed, and opened. More details are required.
            • 4. Re: run function everytime a component is viewd / selected
              ljonny18 Level 1
              Ok – sorry I am reasonably new to flex, so my terminology and descriptions may be a bit off when trying to explain myself….. let me try again.

              My main MXML Application file contains an ApplicationControlBar containing a ViewStack that then links to the other components within my application etc... :

              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:mi="components.*" layout="absolute">

              <mx:ApplicationControlBar id="appBar_apc">
              <mx:LinkBar dataProvider="menu_vs"/>
              </mx:ApplicationControlBar>

              <mx:ViewStack id="menu_vs">
              <mi:page1 id="page_1" label="page1"/>
              <mi:page2 id="page_2" label="page2"/>
              <mi:page3 id="page_3" label="page3"/>
              <mi:page4 id="page_4" label="page4"/>
              </mx:ViewStack>
              </mx:Application>

              then when one of the "links" is selected for the 1st time.... if I call a function on the creationComplete event and everything works fine and as it is supposed to.... but after the page has been created the creationComplete event will not run again - for example if I leave the page / component and return to it.......
              I need the function to run every time the component is "returned to" whether it has been created or not, because in some cases I send ID's and other variables through DataGrid click events etc....


              Do you understand what I am trying to do / what I am getting at…. ?

              Thanks, and sorry for any inconvenience,
              Jon.
              • 5. Re: run function everytime a component is viewd / selected
                inlineblue Level 1
                You can make use of the ViewStack's "change" event. eg:

                <mx:ViewStack id="menu_vs" change="onChange(event)">

                import mx.events.IndexChangedEvent;
                private function onChange(event: IndexChangedEvent): void
                {
                trace(menu_vs.selectedChild.label);
                }

                The event will fire everytime the user changes the page. You've also got more state info in the IndexChangedEvent class (check the docs). The only thing is that this doesn't fire on first creation (since a page hasn't changed). You'll have to take care of that case.
                • 6. Re: run function everytime a component is viewd / selected
                  ljonny18 Level 1
                  Hi inlineblue,

                  I understand what you say about the "IndexChangedEvent" and have tried it and it works, although it in my caase I want to run a function that is within the .mxml file that the Viewstack changes to / displays, not the main application .mxml file where the viewstack is being initialised etc....


                  do you understand what I mean??

                  any suggestions?

                  thanks
                  Jon.
                  • 7. Re: run function everytime a component is viewd / selected
                    ntsiii Level 3
                    Same thing. Instead of:
                    trace(menu_vs.selectedChild.label);

                    something like:
                    trace(menu_vs.selectedChild.someFunctionInChild());

                    Tracy