5 Replies Latest reply on Sep 21, 2006 11:41 AM by inlineblue

    viewStack and child

    Kenneth Fowler Level 1
      I have a main application mxml page with a viewstack container that has other mxml pages as children. Can I access a public function on the child page using actionscript on the main application page?

      Thanks
        • 1. Re: viewStack and child
          peterent Level 2
          The short answer is "yes". For example, if you have a public function called "test" that's on a child of the view stack in your main application:

          <mx:Application ... >
          <mx:ViewStack ...>
          <local:SomeChild id="page1" ... />
          <local:AnotherChild id="page2" ... />

          You can directly reference page2.test() from the main application.

          However, the ViewStack doesn't normally create all of its children until you navigate to them. So you function may not exist or may use undefined data. Set creationPolicy="all" on the ViewStack to have it create all the children at once.

          Now, my question: why would you want to do that? There are some good reasons to do it, but there may be better ways to achieve what you want.
          • 2. Re: viewStack and child
            mac_martine Level 1
            I assume the 'pages' you're refering to are components?

            You should just be able to say:
            myComponentID.functionName();

            I think by default you will only be able to access the components that are in the currently shown stack container and that setting creationPolicy to "all" should eliminate that issue.

            -Mac
            • 3. Re: viewStack and child
              Kenneth Fowler Level 1
              Thanks Pete and Mac

              I'm using a applicationControlBar to run functions on the child pages. If you have a better way please let me know.

              Thanks
              Ken
              • 4. Re: viewStack and child
                mianor64121
                quote:

                but there may be better ways to achieve what you want.


                I have the same problem. I use a ViewStack in which there are views. In a search view, I retrieve a list of results from Java backend. Now, I want to pass these results to another view, say result view.
                Basically I do something like this in the search view :
                main.viewResult.resultList = results;

                But at runtime (just the first time) an error occurs :
                Error #1009: Cannot access a property or method of a null object reference

                quote:

                the ViewStack doesn't normally create all of its children until you navigate to them.

                That's why error message #1009 is diplayed only the first time... I try to use creationPolicy="all" but I have other issues using it and it seems not to be a good practise.
                I'd like to know if there's a better way to pass variables through differents views of a ViewStack... like sharing data or whatever ?
                • 5. Re: viewStack and child
                  inlineblue Level 1
                  One approach is to keep common data at the ViewStack's level and pass a reference to that common data to each child. For example, you can give each child a property called "commonData" and then do something like:

                  [Bindable]
                  private var myData: Object; //data held at the ViewStack's level

                  <mx:ViewStack>
                  <Child1 commonData="{myData}"/>
                  <Child2 commonData="{myData}"/>
                  </mx:ViewStack>

                  This is the general idea. It's not very useful as is.