14 Replies Latest reply on Apr 27, 2009 11:30 AM by Michael Borbor

    Change a state in main.mxml from a script in another mxml

    pingalaEIRE

      Hi,

       

      i'm having some trouble with my application, let's say i have the main application file called MAIN.MXML

      in wich i have set different States, and have different forms.

       

      now i have a form called loginForm which contains the textInput item for Login and Password, i also have a link Need to Register

      which onClick will change the state to show in the same panel a different form registerForm (with the relevant textInput items)

       

      now the MAIN.MXML includes a custom component called <comp: NetworkManager /> which includes all the logic scripts and MXML

      tags to create and manage an HTTPService request to a PHP Server.

       

      when the user click on the Login button, this will call a function included in the main.as script (imported in MAIN.MXML) which

      will call a function in the script within NetworkManager.MXML passing all the form data.

       

      in NetworkManager.MXML the script will create an XML message, will send to the PHP Server and wait the response... if this comes

      true the user is authenticated, otherwise is not.

       

      so far so good, i get the message sent, the correct response back. Now i want to hide the form in MAIN.MXML and put something like

      Welcome firstName lastName!! instead. But i don't know how to do it. If i try to access the states in MAIN.MXML from the NetworkManager.MXML

      i'll get all sort of errors which i don't have it here with me.

       

      I know this sounds like a newbie question, and it really is ... any taker?

      your help is much appreciated.

       

      thanks

      T.

        • 1. Re: Change a state in main.mxml from a script in another mxml
          ATIF FAROOQ Level 3

          Hi,

             will it be appropriate if you also mention the error and How are you accessing script in Main.mxml ?

          • 2. Re: Change a state in main.mxml from a script in another mxml
            Madhav Subedi Level 4

            try following:

             

             

            import mx.core.Application;

            .

            .

            .

            .

            Application.application.currentState="newState";

            1 person found this helpful
            • 3. Re: Change a state in main.mxml from a script in another mxml
              Barna Biro Level 3

              Hi there,

               

              Usually, the best way to do stuff like this is to work with events. Define a custom Event is needed that will store different types so that you'll always know the nature of the currently dispatched event. In your main application, simply add an event listener that subscribes to these events ( example: this.addEventListener(ChangeEvent.LOGIN_FINISH, onLoginFinish); where "onLoginFinish" will be responsible for changing the state of the main application; since this event listener is being set in the main application, you don't have to worry about scope ) and reacts acordingly.

               

              So, when you logging proccess has ended, you could simply dispatch an event ( example: dispatchEvent(new ChangeEvent(ChangeEvent.LOGIN_FINISH)); ) that will bubble up to the event listener that is waiting for it ( since your NetworkManager is a part of the main application mxml, your dispatched event will surely reach the listener ), signaling this way a change in the application.

               

              In case you are new to Events then read up on Adobe LiveDocs.

               

              Best regards,

              Barna Biro

               

              PS: subedimadhav's solution should be correct too just that events are much more ellegant.

              • 4. Re: Change a state in main.mxml from a script in another mxml
                pingalaEIRE Level 1

                Hi,

                 

                thanks all for your reply.

                 

                the last one seems the best approach and should be used as best practices.

                 

                and you also explained so well the event, that i've finally understood how they works ... it was bit obscure at my first read throughout the documentation.

                 

                i will have a try later on, eventually close this thread as solved.

                 

                thanks

                T.

                • 5. Re: Change a state in main.mxml from a script in another mxml
                  pingalaEIRE Level 1

                  Hi,

                   

                  i've used the events, and i can get the state to change from within the scripts in NetworkManager.MXML,

                  however if it occurs i change state again for opening a different view, i've the loginForm coming back ....

                   

                  is there a way once a user is logged, to permanently remove something ? like the loginForm or any other element?

                  so i can always show the Welcome Message?

                   

                  thanks again.

                  • 6. Re: Change a state in main.mxml from a script in another mxml
                    Barna Biro Level 3

                    Hi again,

                     

                    Of course there's a way to remove an element definitely. You can use removeChild(); and remove the specified element from the display list. A better solution would be to work entirely with states if that's the case and check on application creationComplete if there's a user logged in or not, if there is one logged in then instead of showing him the default state with the login form you could simply jump over it to the "welcome state" ( or how you have it named ). Even within states, you can use the RemoveChild MXML tag to remove a certain component if you don't want it to show up in a different state.

                     

                    Another way to do all this would be with pop-ups. Instead of having states, you could maybe have the login form as a custom component and by using the PopupManager you could simply add it to the display list only when needed and remove it with the help of the same manager once you don't need it anymore. The possibilities are almost endless, you just need to pick the one that suits your problem the best way.

                     

                    Best regards,

                    Barna Biro

                    • 7. Re: Change a state in main.mxml from a script in another mxml
                      Michael Borbor Level 4

                      You could use RemoveChild to delete a display object.

                       

                      Sincerely,

                       

                      Michael

                       

                      El 27/04/2009, a las 11:43, pingalaEIRE <forums@adobe.com> escribió:

                       

                      >

                      Hi,

                      >

                      i've used the events, and i can get the state to change from within 

                      the scripts in NetworkManager.MXML,

                      however if it occurs i change state again for opening a different 

                      view, i've the loginForm coming back ....

                      >

                      is there a way once a user is logged, to permanently remove 

                      something ? like the loginForm or any other element?

                      so i can always show the Welcome Message?

                      >

                      thanks again.

                      >

                      • 8. Re: Change a state in main.mxml from a script in another mxml
                        pingalaEIRE Level 1

                        Hi,

                         

                        i've tried RemoveChild but i get an error like:

                        ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

                        at flash.display::DisplayObjectContainer/removeChild()

                        at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::$removeChild()[C:\autobuild\3.2.0\frameworks\p rojects\framework\src\mx\core\UIComponent.as:5096]

                        at mx.core::Container/removeChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\ core\Container.as:2274]

                        at components::NetworkManager/sendUserAuthHandler()[C:\Documents and Settings\Tom\My Documents\flex_prj\Shoppy\src\components\NetworkManager.mxml:182]

                        at components::NetworkManager/serverHandler()[C:\Documents and Settings\Tom\My Documents\flex_prj\Shoppy\src\components\NetworkManager.mxml:98]

                        at components::NetworkManager/__serverRPC_result()[C:\Documents and Settings\Tom\My Documents\flex_prj\Shoppy\src\components\NetworkManager.mxml:290]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\framewor ks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]

                        at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\ projects\rpc\src\mx\rpc\AbstractInvoker.as:193]

                        at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responde r.as:43]

                        at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\ AsyncRequest.as:74]

                        at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\s rc\mx\messaging\channels\DirectHTTPChannel.as:403]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at flash.net::URLLoader/onComplete()

                        who is the caller of loginForm then? i've tried with Application.application.loginForm but i do get the same error though.
                        loginForm is contained in a Canvas item, is it the caller? or who else?
                        thanks
                        T.

                        • 9. Re: Change a state in main.mxml from a script in another mxml
                          Barna Biro Level 3

                          That error usually means that you are not giving the correct path to the object you are trying to remove. Can't guess how you code looks like but you should dinifitely recheck the scope and make sure that what you are trying to remove is actually there and it's accesible.

                          • 10. Re: Change a state in main.mxml from a script in another mxml
                            pingalaEIRE Level 1

                            Hi,

                             

                            the loginForm is a <mx:Form> element in main.mxml if from the script within networkmanager.mxml i do something like

                            Application.application.loginForm.label i can access it, but if i use it (Application.application.loginForm) as argument

                            in the removeChild function it does give me that error.... so i guess is a child of somebody else.

                             

                            is there a way i can check this easily?

                             

                            thanks

                            T.

                            • 11. Re: Change a state in main.mxml from a script in another mxml
                              Michael Borbor Level 4

                              You'll probably will to something like 

                              Application.application.removeChild(loginForm)

                               

                              Sincerely,

                               

                              Michael

                               

                              El 27/04/2009, a las 12:38, pingalaEIRE <forums@adobe.com> escribió:

                               

                              >

                              Hi,

                              >

                              the loginForm is a <mx:Form> element in main.mxml if from the script 

                              within networkmanager.mxml i do something like

                              Application.application.loginForm.label i can access it, but if i 

                              use it (Application.application.loginForm) as argument

                              in the removeChild function it does give me that error.... so i 

                              guess is a child of somebody else.

                              >

                              is there a way i can check this easily?

                              >

                              thanks

                              T.

                              >

                              • 12. Re: Change a state in main.mxml from a script in another mxml
                                pingalaEIRE Level 1

                                Michael i'm afraid it doesn't work.

                                 

                                it says loginForm is not defined as variable, i've also tried Application.application.removeChild(Application.application.loginForm)

                                with no luck still get the original error (loginForm is not a valid child)

                                 

                                so it must be the child of somebody else... i need to find out who delivered that baby !!

                                • 13. Re: Change a state in main.mxml from a script in another mxml
                                  Barna Biro Level 3

                                  Try reproducing the problem in a smaller project and see if you can solve it there. Once you can make it work there, you'll understand how to make it work in your larger project. Or, you can at least post us some source code so that we can actually see what's going on there and where you have put your code.

                                   

                                  Best regards,

                                  Barna Biro

                                  • 14. Re: Change a state in main.mxml from a script in another mxml
                                    Michael Borbor Level 4

                                    Yes you're right it doesn't work to find the best solution probably 

                                    you could post a bit of your code to see where it's going wrong

                                     

                                    Sincerely,

                                     

                                    Michael

                                     

                                    El 27/04/2009, a las 12:54, pingalaEIRE <forums@adobe.com> escribió:

                                     

                                    >

                                    Michael i'm afraid it doesn't work.

                                    >

                                    it says loginForm is not defined as variable, i've also tried 

                                    Application.application.removeChild(Application.application.loginForm)

                                    with no luck still get the original error (loginForm is not a valid 

                                    child)

                                    >

                                    so it must be the child of somebody else... i need to find out who 

                                    delivered that baby !!

                                    >