2 Replies Latest reply on Nov 6, 2007 2:20 PM by wlee@unimelb

    Accessing parent application XML data

    wlee@unimelb
      Can anyone briefly point me in the right direction with respect to this problem:

      I have several custom Canvas classes, which are instantiated at runtime, based on the data drawn in to the parent application from an external XML file. So far so good. However, I need to pass that same XML information (or parts of it) down to the custom canvas, for display in Text objects within the custom canvas.

      My hierarchy is this:

      Application
      > XML object (the data in question)
      > DynamicPanel class
      > TabNavigator
      > Canvas
      > Custom Canvas class (one of four types)

      I've tried all manner of parentDocument and parentApplication tree walking (none of which seems to yield any results), as well as the Application.application.wroteAFunctionHere() approach. I'm not sure, maybe I did something wrong?

      Here's my most recent code in the Application:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      xmlns:custom="myComponents.*"
      creationComplete="init()" xmlns:local="*">

      <mx:XML source="compiled.xml" id="quizQns" />
      <mx:Script>
      <![CDATA[

      private var myPanel:DynamicPanel;

      public function init():void
      {
      myPanel = new DynamicPanel(quizQns);
      addChild(myPanel);
      }

      public function getQuestion(indexWanted:int):XML
      {
      var subset:XML = new XML();
      subset = quizQns.question[indexWanted];

      return subset;
      }


      ]]>
      </mx:Script>


      </mx:Application>

      And the custom canvas class (please ignore the feedback stuff - it was working in the non-dynamic version, but needs updating):

      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="300" backgroundColor="#ff7e7b" creationComplete="init();">
      <mx:states>
      <mx:State name="feedback">
      <mx:AddChild position="lastChild">
      <mx:Text text="{feedbackString}" left="10" top="{myVBox.y + myVBox.height +30}" id="feedbackText"/>
      </mx:AddChild>
      </mx:State>
      </mx:states>
      <mx:Script>
      <![CDATA[

      import mx.core.Application;

      [Bindable]
      public var feedbackString:String;
      [Bindable]
      public var xmlURL:String;
      [Bindable]
      public var indexNo:int;

      [Bindable]
      public var xmlCopy:XML;

      public function init():void
      {
      xmlCopy = new XML(Application.application.getQuestion());
      }

      public function giveFeedback() : void
      {

      if(mcqOpts.selection.value == parentDocument.quizQns.question[indexNo].answer.value)
      {
      feedbackString = parentDocument.quizQns.question[indexNo].items.item.(@value == mcqOpts.selection.value).feedback;
      feedbackString += "\n" + parentDocument.quizQns.question[indexNo].questionFeedback;
      }
      else
      {
      feedbackString = "That's incorrect. " + mcqOpts.selection.label + " is not right. Try again.";
      }
      //feedbackText.text = feedbackString;
      currentState ='feedback';
      }
      ]]>
      </mx:Script>

      <mx:Text left="10" top="10" id="dummyText" text="HELLO" right="10"/>
      <mx:Text left="10" top="20" id="questionText" text="{xmlCopy.valueOf()}" right="10"/>
      <mx:VBox left="10" top="{questionText.y + questionText.height + 10}" id="myVBox">
      <mx:RadioButtonGroup id="mcqOpts" change="{giveFeedback()}"/>
      <mx:Repeater dataProvider="{parentDocument.quizQns.question(indexNo).items.item}" id="options">
      <mx:RadioButton label="{options.currentItem.option}" value="{options.currentItem.@value}" groupName="mcqOpts" left="10"/>
      </mx:Repeater>
      </mx:VBox>

      </mx:Canvas>


      HELP?!?
        • 1. Re: Accessing parent application XML data
          Pkeating
          You know, this actually works fine if you get rid of the compile-time errors.

          1) remove the argument from the DynamicPanel constructor: myPanel = new DynamicPanel();
          2) remember to pass an int to getQuestion: xmlCopy = new XML(Application.application.getQuestion(0));

          Also, just for fun, you might want to consider two other things when you have time:

          1) Consider loading your XML in an asynchronous manner, meaning that you set an eventListener for XML load, load the xml, and then update your panel strings only after a successful XML load event. (a good chance to use data binding) That way your xml could be loaded at run time (from a web server or the local system) which has some obvious upsides.

          2) You could put your XML and getQuestion() method in its own class and use that as the 'model' in an MVC pattern. (google MVC + Flex + Cairngorm) That way you can load an instance of the model class (as a singleton) instead of accessing it every time through Application.application

          Have fun!

          -patrick

          • 2. Re: Accessing parent application XML data
            wlee@unimelb Level 1
            Wow, thanks! That totally solved my problem! My deadline's a little tight, so I might look into the 'fun' advice a little later.

            Thank you thank you thank you thank you!!!