5 Replies Latest reply on Aug 28, 2008 7:43 PM by Ratsnackbar

    Dynamic View Stack

    Mike_Eppel
      Hi all,

      I am trying to create a reusable wizard application component around the ViewStack component. The component will make it easy for people to create a new wizard by defining paths through the wizard in XML along with the names of the views/pages, and then implementing the views/pages in mxml.

      Is there a way to add named views, implemented as mxml pages (probably based on Canvas), at runtime? The trick seems to be creating a DisplayObject from an arbitrary name that maps to the actual mxml file. Is this even possible? Does the ViewStack require all the views to be defined statically?

      Below is the XML that defines the wizard.
        • 1. Re: Dynamic View Stack
          Gregory Lafrance Level 6
          In ActionScript, just create an instance of a component and add it to the ViewStack with addChild() or addChildAt().

          import components.MyFancyComponent;

          private var mfc:MyFancyComponent = new MyFancyComponent();

          myViewStack.addChild(mfc);
          • 2. Re: Dynamic View Stack
            Level 7

            "Mike Eppel" <webforumsuser@macromedia.com> wrote in message
            news:g96m8h$9gg$1@forums.macromedia.com...
            > Hi all,
            >
            > I am trying to create a reusable wizard application component around the
            > ViewStack component. The component will make it easy for people to create
            > a new
            > wizard by defining paths through the wizard in XML along with the names of
            > the
            > views/pages, and then implementing the views/pages in mxml.
            >
            > Is there a way to add named views, implemented as mxml pages (probably
            > based
            > on Canvas), at runtime? The trick seems to be creating a DisplayObject
            > from an
            > arbitrary name that maps to the actual mxml file. Is this even possible?
            > Does
            > the ViewStack require all the views to be defined statically?

            Use getDefinitionByName and ClassFactory to dynamically create your class.
            Just be sure that you actually have at least one "hard" reference to each
            class you intend to use, or the class might not get compiled into your swf.
            Here's a full write-up of how to do that:
            http://www.paulofierro.com/archives/520/


            • 3. Re: Dynamic View Stack
              Mike_Eppel Level 1
              Hi Amy,

              Thanks for the help. That is exactly what I was looking for. I put the components under a /views folder and added an import views.*; statement. When the line

              var viewRef:Class = getDefinitionByName(view) as Class;

              is executed with view="welcome" I get an Error #1065: Variable welcome is not defined. I tried adding "hard" references to the components in the application mxml file, but no joy. Thoughts?
              • 4. Re: Dynamic View Stack
                Level 7

                "Mike Eppel" <webforumsuser@macromedia.com> wrote in message
                news:g96pbu$cqq$1@forums.macromedia.com...
                > Hi Amy,
                >
                > Thanks for the help. That is exactly what I was looking for. I put the
                > components under a /views folder and added an import views.*; statement.
                > When
                > the line
                >
                > var viewRef:Class = getDefinitionByName(view) as Class;
                >
                > is executed with view="welcome" I get an Error #1065: Variable welcome is
                > not
                > defined. I tried adding "hard" references to the components in the
                > application
                > mxml file, but no joy. Thoughts?

                You missed the ClassFactory step. Check the link I posted for more details.

                HTH;

                Amy


                • 5. Re: Dynamic View Stack
                  Ratsnackbar Level 2
                  I use the same method to dynamically load tabbed or window views with AS3 or MXML applets. It works pretty well but if you get carried away and your project could grow very fast and become more difficult to debug. Plus it is often harder to track down memory leaks. So use with care.

                  For smaller to medium projects just add one import statement and one var set to null for each class to a flat .as file and include the file into your main class. One easy to find central location.

                  For larger projects you might want to think instead about using loadable modules that share RSL's.