20 Replies Latest reply on Mar 17, 2011 4:33 PM by majeric

    How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?

    majeric

      Suppose I have something that looks like:

       

      <?xml version="1.0" encoding="utf-8"?>

      <ex: MyContainer

      xmlns:mx="http://www.adobe.com/2006/mxml"

      xmlns:ex="example.*"

      >

       

           <ex:MyObject label="object 1" />

           <ex:MyObject label="object 2" />

      </ex:MyContainer>

       

      MyContainer and MyObject are classes. I would expect that MyContainer would have to conform to an interface such that it would contain a set of MyObject but this doesn't appear to be the case. Both objects conform to the IMXMLObject interface so they are compiled and they do get instantiated by the runtime.... but I lack the assocation between MyContainer and MyObject that I need.

       

      Any suggestions?

       

      Cheers,

      Jeremy

        • 1. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
          JeffryHouser Level 4

          Lots of stuff going on here. In short, read up on the Flex Component LifeCycle.  It doesn't start until after a child is added to the stage.  If you never create an instance of MyContainer and add it as a child, it'll never go through it's own component lifecycle and never create the children.

           

          A but more thoughts:

           

          I would expect that MyContainer would have to conform to an interface such that it would contain a set of MyObject but this doesn't appear to be the case.

          Based on the code sample, you didn't create or extend an interface, so why would you expect MyContainer to conform to one?

          If an interface were to be created automatically/under the hood; why would the "MyContainer" interface contain a set (Array?) of MyObjects?  As far as it knows, they are just children.  If you want to access an array of MyObjects, you'll have to create that array and add/ remove items to it in your code.  Look at the way that a columns are defined in a DataGrid using MXML as one example of this.

           

          Does that help?

          • 2. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
            majeric Level 1

            Both MyContainer and MyObject are AS classes. They implement the IMXMLObject interface. I threw some trace statements in the constructor and I get instantiations for both of them... My issue is that when Flex instantiates my classes, I expect that because MyObject is a child of MyContainer that Flex would add it in some way to MyContainer... but perhaps I haven't implemented an additional interface for MyContainer so that it has an "addChild" call or  something.

             

            Does that make my question clearer?

             

            Thanks for the reply.

             

            Jeremy

            • 3. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
              Flex harUI Adobe Employee

              IMXMLObject allows an object to reference its parent document, not not

              necessarily the other way.  If you put an id attribute on that instance via

              MXML markup, the compiler will generate a variable referencing that object.

              If you are using AS, you have to declare your own reference variables.  Only

              displayobjects get added as children.

              • 4. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                majeric Level 1

                So, there's no way to do this? Mate's Map files seem to define non-visual configuration/mapping files. Although I don't know if the parent object contains the child object. The problem I have is that the I would want to map the child objects to an indexed array or a dictionary. I was hoping to use it as a means of defining configuration mapping for my App.

                • 5. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                  Flex harUI Adobe Employee

                  I don't know what Mate's map files do.  And I'm not quite sure I understand

                  the problem you are trying to solve.  You can build up any heirarchy you

                  want if you write enough code.

                  • 6. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                    majeric Level 1

                    Lets see if I can describe more accurately what I want (not complete... just written for brevity):

                     

                     

                    In my Main.mxml:

                     

                    <config:MyConfig/>

                     

                    In a MyConfig.mxml file:

                    //-------------------------------------------

                    <ex: Config xmlns:ex="example.*">

                         <ex:property label="prop1" value="0"/>

                         <ex:property label="prop2" value="catfish"/>

                         <ex:property label=''prop3" value="23.456"/>

                         <ex:property label="prop4" value="true"/>

                    </ex:config>

                     

                     

                    //-------------------------------------------

                     

                    package example {

                    public class Config implements IMXMLObject

                    {

                         var mMap:Dictionary;

                     

                         public function Config

                         {

                              mMap = Dictionary

                         }

                     

                     

                     

                         public function initialized(document:Object, id:String):void

                         {

                         }

                     

                    }

                     

                     

                     

                    //-------------------------------------------

                    public class Property implements IMXMLObject
                    {
                         public var label:String;
                         public var value:String;

                         public function initialized(document:Object, id:String):void

                         {

                         }

                    }
                    }
                    I want to be able to use MXML as a non-visual configuration. However, I am trying to get the map file in the Config class to be populated with the Property Objects...
                    Suggestions?
                    I would have assumed that there was some interface that allowed me to set up a hierarchy in my XML config. That I could add the child "Property" objects to the "Config" Object given that you can do so in components. However, previous posters don't seem to think that this is possible. Which seems really odd to me. It's something that I would think that is missing in Flex.
                    Like if a Non-Visual IMXMLObject also conformed to a "IMXMLContainer" interface (if such a thing existed) that included "addChild" that a reference to the Property Object would be passed through to the Config Object as they get created.
                    Jeremy
                    • 7. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                      Flex harUI Adobe Employee

                      I don't think you can initialize a Dictionary from MXML.

                       

                      You could do something like:

                       

                          [DefaultProperty("properties")]

                          public class Config implements IMXMLObject

                          {

                              public var id:String;

                       

                              public var properties:Array;

                       

                              private var mMap:Dictionary;

                       

                              public function initialized(document:Object, id:String);

                              {

                                  if (properties)

                                  {

                                      var n:int = properties.length;

                                      for (var i:int = 0; i<n; i++)

                                          mMap[properties[i].label] = properties[i].value;

                                  }

                              }

                          }

                       

                      I don't think Properties has to implement IMXMLObject.  The MXML would then

                      look like:

                       

                          <config:Config>

                              <config:Properties label=".." value=".." />

                              ...

                          </config:Config>

                       

                      Note that if you are really only populating an Object, you can just use

                      mx:Object or fx:Object depending on if you are using Flex 3 or 4 and skip

                      having a Properties class at all.

                      • 8. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                        majeric Level 1

                        I have to admit that I'm not quite following this code:

                         

                        [DefaultProperty("properties")]

                         

                        This does what? capitalization doesn't matter? I noticed that "properties" and "Properties" were both used. It feels a little awkward given that one is a single "property" and a collection of them are "properties" but i may just get over that. I could jus encode the config in actionscript directly but the MXML seem to lend itself to defining a config in the way that I was describing.

                         

                         

                        Learning new stuff is interesting because you get to imagine all the potential of something but you often find yourself standing outside the box rather unintentionally.

                         

                         

                        There should be a "Container" interface  So that you IMXMLObjects can refect a DOM like Hierarchy.

                         

                        Cheers,

                        Jeremy

                         


                        • 9. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                          Flex harUI Adobe Employee

                          An MXML file is a document, so you build your tree by enclosing sets of tags

                          in different MXML files.

                           

                          DefaultProperty specifies which property is set by tags within a tag.

                           

                          My email program sometimes capitalizes things it shouldn't.

                          • 10. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                            majeric Level 1

                            It's not immediately working... but I'm still chewing so I might actually just create a quick example project so I have a working copy of what I'm talking about.

                             

                            Thanks for the help thus far.

                             

                            Jeremy

                            • 11. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                              majeric Level 1

                              Alright. This is my Demo project. Perhaps you can see what i mean by the following code. The goal is to be able to reference an instance of Config and access the map within it eventually. This seems like an interesting way of configuring a Flex application. I'm surprised objects constructed this way, don't follow the hierarchy defined in the MXML much like a regular XML DOM would. Or at least provide the option.

                               

                               

                              Main.mxml:

                              //===================================================

                              <?xml version="1.0" encoding="utf-8"?>

                              <WindowedApplication

                              xmlns="http://www.adobe.com/2006/mxml"

                              xmlns:example="example.*"

                              >

                              <example:Configuration/>

                               

                               

                              <visible>false</visible>

                              <width>800</width>

                              <height>600</height>

                              </WindowedApplication>

                              Configuration.mxml:

                              //===================================================

                              <?xml version="1.0" encoding="utf-8"?>

                              <example:Config

                              xmlns="http://www.adobe.com/2006/mxml"

                              xmlns:example="example.*"

                              >

                               

                              <example:Property label="prop1" value="0"/>

                              <example:Property label="prop2" value="catfish"/>

                              <example:Property label="prop3" value="23.456"/>

                              <example:Property label="prop4" value="true"/>

                              </example:Config>

                               

                              Config.as:

                              //===================================================

                              package example

                              {

                               

                              import flash.utils.Dictionary;

                               

                              import mx.core.IMXMLObject;

                               

                              [DefaultProperty("Property")]

                              public class Config implements IMXMLObject

                              {

                               

                              protected var mMap:Dictionary;

                              protected var mProperty:Array;

                              protected var mParentDoc:Object;

                               

                              public function Config()

                              {

                              trace("Config.Config");

                              }

                               

                               

                              //----------------------------------------------------------

                              public function get Property():Array

                              {

                              return mProperty;

                              }

                               

                              //----------------------------------------------------------

                              public function set Property(value:Array):void

                              {

                              mProperty = value;

                              }

                               

                              //----------------------------------------------------------

                              public function initialized(document:Object, id:String):void

                              {

                              trace("Config.initialized:id:" + id);

                              //----------------------------------------------------------

                              this.mParentDoc = document;

                               

                              trace("Config.initialized:" + mProperty);

                               

                              if (Property != null)

                              {

                              mMap = new Dictionary();

                              for(var i:Number = 0; i < mProperty.length; i++)

                              {

                              trace("Config.initialized:" + mProperty[i].label);

                              trace("Config.initialized:" + (mProperty[i] is example.Property));

                               

                              mMap[mProperty[i].label] = mProperty[i].value;

                              }

                              }

                              }

                              }

                              }

                               

                              Property.as

                              //==================================

                              package example

                              {

                              import mx.core.IMXMLObject;

                               

                              public class Property implements IMXMLObject

                              {

                               

                              public var label:String;

                              public var value:String;

                               

                              protected var mParentDoc:Object;

                               

                               

                              public function Property()

                              {

                              trace("Property.Property");

                              }

                               

                              //----------------------------------------------------------

                              public function initialized(document:Object, id:String):void

                              {

                              trace("Property.initialized:id:" + id);

                              this.mParentDoc = document;

                              }

                               

                              }

                              }

                               

                               

                               

                               

                               

                              • 12. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                Flex harUI Adobe Employee

                                I'm not sure what you mean by "don't follow the heirarchy", but  XML is

                                usually all in one file, MXML hierarchy is spread over several files.

                                That's the way we designed it, so you don't have to "dot-down" to get to

                                grandchildren (main.child.grandChild), you can bind to properties instead of

                                functions like getElementById.

                                • 13. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                  majeric Level 1

                                  What I mean by "hierarchy" is that in the MXML it's XML with a hierarchy.

                                   

                                  <MyContainer> <MyObject id="1"/> <MyObject id="2"/> </MyContainter>

                                   

                                  Both MyObject are contained within the MyContainer tag. If this were read into a DOM, then literally there would be two MyObject inside of MyContainer. Because in MXML, MyContainer and MyObject can be both classes, one would reasonably assume that there would be two MyObjects contained within MyContainer because MyContainer would conform to some sort of container interface.

                                   

                                  That's what i mean by "follow the hierarchy".

                                   

                                  Jeremy

                                  • 14. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                    Flex harUI Adobe Employee

                                    All id's are flattened into the document, but the default property of

                                    MyContainer has been set to be an array of the two MyObjects so you should

                                    be able to drill-down into them if you want to.  IOW, if you had this:

                                     

                                     

                                     

                                    And MyContainer's default property was defined as:

                                     

                                        [DefaultProperty("objects")]

                                        public class MyContainer

                                        {

                                            public var objects:Array

                                     

                                    Then the following should also work:

                                     

                                        trace(a.objects[0].id); // b

                                        trace(a.objects[1].id); // c

                                     

                                    The key is knowing what property holds the heirarchy.  For display objects,

                                    there is no property and you use getChildAt or getElementAt

                                    • 15. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                      majeric Level 1

                                      Why is there a distinction between visual objects and non-visual objects?

                                       

                                      Jeremy

                                      • 16. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                        majeric Level 1

                                        in my complete example above, why isn't the mProperty array populated by the Property objects that I've defined? Did I do something wrong?

                                         

                                        Jeremy

                                        • 17. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                          Flex harUI Adobe Employee

                                          Because there is a built-in API for building a tree structure of display

                                          objects.  We don't really want to require non-visual objects to conform to

                                          some API of having parent properties and other overhead, but you can create

                                          the same by using DefaultProperty.

                                          • 18. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                            fancle

                                            可以参考一下flex 4 Group的实现方式,在构造器中mxmlContent =

                                            ;

                                             

                                            2011/3/17 Flex harUI <forums@adobe.com>

                                             

                                            >

                                            Because there is a built-in API for building a tree structure of display

                                            objects.  We don't really want to require non-visual objects to conform to

                                            some API of having parent properties and other overhead, but you can create

                                            the same by using DefaultProperty.

                                            >

                                            • 20. Re: How do you access a Non-Visual IMXMLObject Hierarchy in actionscript?
                                              majeric Level 1

                                              The short answer is that I should probably find a different way of doing what I want  to do. *sigh*.

                                               

                                              Thanks for all your help.

                                               

                                              Jeremy