5 Replies Latest reply on Mar 6, 2008 5:11 PM by SiHoop

    ViewStack

    SiHoop Level 1
      I'm using a ViewStack to create a login system that sends a user to one of three containers according to an access level that is determined at login. When routed to the appropriate container, I need to access a user's id, but I don't know how to pass the id from the the login module to the container.

      So, how do I pass a variable from one ViewStack component to another?
      Thanks!
        • 1. Re: ViewStack
          ntsiii Level 3
          I advise setting up a data model of some kind that is accessible from all of your components.

          This could be simpley instance variables in the application scope, that you access uing Application.application.

          Or it could be a bindable component of its own. Some folks advise using the singleton pattern.

          One thing with ViewStack. Its children are not crated until a user navigates to the view. So you cannot set a property in a child that has not been viewed.

          The way to handle this is to bind the values in the child to the model, or use the child view's creationComplete or show events to fetch the data from the model.

          Tracy
          • 2. Re: ViewStack
            SiHoop Level 1
            Tracy,
            Thanks for your reply. I had tried using local shared objects, but that is a work around.
            In your suggestion to use Application.application, do you mean that I should use use something like the following (where myVar is the variable that I want to access from the original login file)?

            mx.core.Application.application.parameters.myVar

            I tried this, but it returned null.

            I understand what you mean about the limitation of the ViewStack, but have not found any other satisfactory way to break my project into manageable units. Also, do you know of any examples that illustrate setting up a data model that can be accessed from different components.
            • 3. Re: ViewStack
              ntsiii Level 3
              No, do not use sharedObject for this.

              In one of the ViewStack children, declare a handler for the "show" event. Say show="onShow()".

              import mx.core.application; //you need to import this
              private function onShow():void {
              var app:Application = Application.application; //I actually do this as in instance variable in all components
              var myVar:String = app.myVar;
              trace(myVar);
              }


              Also, I am not suggesting you avoid Viewstack. It is the primary architectural feature in almost all of my apps. Just be aware of the deferred instantiation issue, and do not attempt to set properties on a child view from the "outside" Bind, or use an event as above.

              Tracy
              • 4. Re: ViewStack
                ntsiii Level 3
                Add'l note, myVar must be PUBLIC.
                Tracy
                • 5. Re: ViewStack
                  SiHoop Level 1
                  Perfect,
                  Thanks.