4 Replies Latest reply on Nov 17, 2007 6:24 AM by chris.huston.t10

    Load variables within viewstack, AFTER login

    Bas van Ginkel
      Hi there people,

      I've been scratching my head because of this problem for a few hours now. The problem is i've got an application consisting of multiple components loaded through a viewstack. Of course data has to be guarded so users can only get access after login.

      At login a session_id is created serverside for all future references by the user. All verification and access control is handled there. This session_id only available after the user has logged in. The different components send this session_id with every server request. Problem is that the application tries to load data before the user is logged in. (The login is a state in the main application, Components load data as creationComplete).

      How do i do the following:
      1. Stop the firing of the viewStack Components creationComplete event before the session_id is available? Or how do i mimic the creationComplete so that it load when a Component is selected from the viewstack?
      2. How i parse the session_id variable from the main application to the components? I now have the variables inside the viewstack (see below) but now an empty var is parsed because the viewstack is created along side the login state.

      <CODE>
      <mx:ViewStack id="MainApp">
      <v:Cars id="Cars" session="{session}" label="Cars"/>
      <CODE>

      I really hope someone can help me,
      Bas van Ginkel
        • 1. Re: Load variables within viewstack, AFTER login
          chris.huston.t10 Level 3
          I would have a function that is called when there is a successful login by a user. This function would then set your session_id variable, change the currentState to the viewstack section of the app, and then make a call to your server to get the data you need for each component. This way you do not need to rely on creationComplete events. Use dataBinding for components on viewstack views that are not currently visible. There is also a show event that might be of use, depending on your app's structure. If you are still having trouble, try to post a bit more of your code so we can give more focused help.

          Vygo
          • 2. Load variables within viewstack, AFTER login
            Bas van Ginkel Level 1
            Masamune, thanks for youre reaction. The structure you suggest if already present in my application. I will post the basic structure below so you could perhaps help me figure this one out. I haven't posted my entire code because i think the posting below will beter help you understand my structure. I can't seem to get my head around a design pattern that works within Flex.


            <APPLICATION CODE currentState="Login">

            <MX:RemoteObject>
            Called by the Login function that handles the form field
            </MX:RemoteObject>

            <MX:Script>
            <VARIABLE>
            public var session_id:String (This is to hold the session_id returned by the login script)
            </VARIABLE>
            <FUNCTION>
            Function for handling the ResultEvent from the RemoteObject. If login is succesfull the session_id is assigned to a variable and the currantState changes to the one displaying the ViewStack.
            </FUNCTION>
            <FUNCTION>
            Function for calling the RemoteObject if the signin button is clicked by the user.
            </FUNCTION>
            </MX:Script>

            <MX:States>
            <MX:State name="Login">
            Removes all Children and adds a panel with the login screen.
            </MX:State>
            </MX:States>

            <MX:Linkbar>With the buttons for the different parts of the ViewStack</MX:Linkbar>

            <MX:ViewStack>
            <v:Cars id="Cars" session_id="{session_id}" label="Cars"/>
            <v:Drivers id="Drivers" session_id="{session_id}" label="Drivers"/>
            ...
            </MX:ViewStack>

            </APPLICATION CODE>




            <COMPONENT CODE CARS creationComplete="initModule()">

            <MX:RemoteObject>
            Called by the initModule function and retrieves the data needed for this module
            </MX:RemoteObject>

            <MX:Script>
            <VARIABLE>
            public var session_id:String
            </VARIABLE>
            <VARIABLE>
            private var CarList:ArrayCollection (To hold the results from the RemoteObjectResult(event)
            </VARIABLE>
            <FUNCTION>
            Initmodule function to call the RemoteObject
            </FUNCTION>
            </MX:Script>

            <MX:DataGrid>
            Displaying the content of the CarList:Arraycollection
            </MX:DataGrid>

            </COMPONENT CODE>


            The present problem is that the ViewStack is loaded before the session_id is available, because of this the Component receives a blank session_id from the main application. This blank session_id is directly used by the RemoteObject that fires before the login is completed.

            Is there a way, perhaps by not using a viewstack or something, to first complete the login before ViewStack and Components are initiated? Or can someone suggest a adjustment in structure to achieve the same result (that being a working application)?
            • 3. Re: Load variables within viewstack, AFTER login
              ntsiii Level 3
              You could have the login in the "top" view of the viewstack, instead of a state.

              This way, the other views are not instantiated until successful login permits navigation to them. ViewStack uses deferred instantiation by default, and the child views are not rendeed until a user navigates to them.

              Tracy
              • 4. Re: Load variables within viewstack, AFTER login
                chris.huston.t10 Level 3
                How about if instead of calling initModule from the creationComplete event in your components, you made your initModule functions public and called them from your function that responds to a successful login and sets your session_id? This would seem to solve your problem.

                currentState = viewstack section;
                session_id= some value;
                Cars.initModule();
                Drivers.initModule();

                Vygo