4 Replies Latest reply on Dec 20, 2010 11:44 AM by adamski52

    Referencing ViewStack's inner content

    adamski52

      Hi all - I have a question which I'm sure has a simple answer, but I'm just not seeing it and I'm spinning my wheels.  One of those things that I'm too close to see, I'm sure.

       

      Consider the following which is working:

      • I'm loading an XML file at run-time (actual run time, not Flex's embedded runtime).
      • For each item in that XML file, I'm creating a new SWFLoader.
      • Each SWFLoader is added to a ViewStack.
      • Each SWFLoader is a class called "ChildApplication" which extends s:Application.
      • There exists a TabBar.
      • Depending on attributes in that XML file, the SWFLoader is or is not loaded in to that TabBar.
        • If the SWFLoader is loaded in to the TabBar, its ID as dictated by the XML file is added to an ArrayCollection acting as a ID to index map of the TabBar since the indices of the ViewStack and the TabBar may not necessarily match.
        • Because each item is inside of a ViewStack it appears it is being wrapped as a NavigatorContent object.

       

      The objective:

      • I need to have the parent (root of the hierarchy) talk to the ChildApplications within the ViewStack.  After all the loading is done, their structure is as follows: <ViewStack><NavigatorContent><SWFLoader (itself an Application, extended as ChildApplication) /></NavigatorContent>...</ViewStack>

       

       

      The problem:

      • I'm unable to get a reference to the ChildApplication class to call a public method.

       

       

      What I've tried that does not work:

      • Using ViewStack.selectedChild (this gives NavigatorContent).
      • Explicitly casting the ViewStack.selectedChild as ChildApplication and Application.
      • Bubbling an event from the parent application to all ChildApplication classes, overriding that listener in each individual application to do the necessary task.
        • This is less than optimal because I'd need to remove that listener from each ViewStack Application on IndexChange of the TabBar so that only the active one responds, but I can't because I can't get a reference to each item.

       

      I cant share code but hopefully that's pretty clear...  any ideas?

        • 1. Re: Referencing ViewStack's inner content
          TomBurgundy Level 1

          Have you tried

           

          getChildAt(index:int):DisplayObject

           

          for the NavigatorContent Komponent? I believe (haven't checked) that this function is declared private in the ASDOC, so it doesnt show in content-assist and the docs, but can still be called..

          1 person found this helpful
          • 2. Re: Referencing ViewStack's inner content
            adamski52 Level 1

            I tried that.  It gives me what I want, but it seems the way I'm doing it without that gives the same result a little easier.  The relevant code, redacted a bit, is as follows:

             

            public function gotoView(i:uint = 0):void {

               MainApp(this).nav.selectedIndex = i; // set the TabBar index according to which view in the map we wish to see.

             

               var item:NavigatorContent = MainApp(this).mainView.selectedChild = NavigatorContent(MainApp(this).mainView.getChildByName(navMap[i])); // set the selectedChild and a reference to that child as the view's child-by-name according to the navigation mapping.

             

               trace(item.getChildAt(0)); // MainApp.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.mainView.welcome.SkinnableContainerSkin23

               trace(getQualifiedClassName(item.getChildAt(0))); // spark.skins.spark::SkinnableContainerSkin

             

               // "welcome" is the ID of the currently active view in the ViewStack.  Its this application whose public method I need to call.

             

               trace(item.getChildAt(0).parent); // MainApp.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.mainView.welcome

               trace(getQualifiedClassName(item.getChildAt(0).parent)); // spark.components::NavigatorContent

             

             

               // the above is the same as just

               trace(item);  // MainApp.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.mainView.welcome

               trace(getQualifiedClassName(item)); // spark.components::NavigatorContent

             

               // what I'm trying to say that does not work:

               ChildApplication(item).init(); // init is a public method.  fails because it cannot convert NavigatorContent to ChildApplication.  I don't want to convert it anyway.  What I want is basically that NavigatorContent's content...

            }

            • 3. Re: Referencing ViewStack's inner content
              adamski52 Level 1

              After some thought, it might be the case that this just isn't allowed for security purposes.  Similar to how in JavaScript, you cannot mess with an IFRAME's content.  I suppose there is nothing stopping me from pulling in a remote site's SWF and trying to act upon it if what I'm trying to do were possible.  Or, even worse, somebody taking the child SWFs and including it in their own malicious parent container.

               

              Is there perhaps a better way to accomplish what I'm trying to do?

              • 4. Re: Referencing ViewStack's inner content
                adamski52 Level 1

                Got it.

                 

                Solution:

                var item:NavigatorContent = NavigatorContent(MainApp(this).mainView.selectedChild;

                ChildApplication(SystemManager(SWFLoader(item.getElementAt(0)).content).application).init( );

                 

                That will call the public init() method of MainApp's ViewStack's selectedChild, which is nested inside a NavigatorContent item, which contains a SWFLoader loaded at run-time.