2 Replies Latest reply on Aug 31, 2007 6:52 AM by aceinc

    Going a bit crazy...

    aceinc
      Sorry for the long post, but it is the shortest I could make it and still demonstrate the problem.

      The purpose of this excercise is to add items to menus by modifying the underlying XML in the dataprovider. I would like the sample code below to add 1 item to each menu everytime I click the "Add Menu Item" button. It actually does this the first time. After the first time however, it seems to always place the new menu items under Menu3.

      Below you will find the offending code, and the output from the trace statements. I have been working on this for quite a bit, and it is truly driving me bonkers. I would like to say it is a bug, but I am not confident that it isn't my misunderstanding.

      If someone would shed some light on this, I would be very appreciative.

      Paul

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="absolute">
      <mx:XML id="menuData" format="e4x">
      <root>
      <MenuItem label="Menu1">
      <SubItem label="SubItem1"/>
      </MenuItem>
      <MenuItem label="Menu2">
      <SubItem label="SubItem2"/>
      </MenuItem>
      <MenuItem label="Menu3">
      <SubItem label="SubItem3"/>
      </MenuItem>
      </root>
      </mx:XML>
      <mx:Script>
      <![CDATA[
      [Bindable] public var counter:int = 3;
      // [Bindable] public var menuData:XML = menuXML;
      private function addMenuItem():void
      {
      trace("---------------------Before-------------------------");
      trace(menuData);
      counter += 1;
      var lab:String = '<SubItem label="SubItem ' + counter + '" />';
      var vxml:XML = new XML(lab);
      // add the new panel to the menu list of open panels
      menuData.MenuItem[0].appendChild(vxml);
      menuData.MenuItem[1].appendChild(vxml);
      menuData.MenuItem[2].appendChild(vxml);
      trace("---------------------After-------------------------");
      trace(menuData);
      menu1.dataProvider = menuData;
      }
      ]]>
      </mx:Script>
      <mx:MenuBar x="36" y="15" width="510" dataProvider="{menuData}" id="menu1" showRoot="false" labelField="@label"/>
      <mx:Canvas
      width="100%" height="53" bottom="0"
      backgroundColor="#efefef"
      borderSides="top" borderThickness="3" borderColor="#666666" borderStyle="solid">
      <mx:Button right="10" verticalCenter="0" label="Add Menu Item" click="this.addMenuItem()"/>
      </mx:Canvas>
      </mx:Application>


      ---------------------Before-------------------------
      <root>
      <MenuItem label="Menu1">
      <SubItem label="SubItem1"/>
      </MenuItem>
      <MenuItem label="Menu2">
      <SubItem label="SubItem2"/>
      </MenuItem>
      <MenuItem label="Menu3">
      <SubItem label="SubItem3"/>
      </MenuItem>
      </root>
      ---------------------After-------------------------
      <root>
      <MenuItem label="Menu1">
      <SubItem label="SubItem1"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu2">
      <SubItem label="SubItem2"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu3">
      <SubItem label="SubItem3"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      </root>
      ---------------------Before-------------------------
      <root>
      <MenuItem label="Menu1">
      <SubItem label="SubItem1"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu2">
      <SubItem label="SubItem2"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu3">
      <SubItem label="SubItem3"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      </root>
      ---------------------After-------------------------
      <root>
      <MenuItem label="Menu1">
      <SubItem label="SubItem1"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu2">
      <SubItem label="SubItem2"/>
      <SubItem label="SubItem 4"/>
      </MenuItem>
      <MenuItem label="Menu3">
      <SubItem label="SubItem3"/>
      <SubItem label="SubItem 4"/>
      <SubItem label="SubItem 5"/>
      <SubItem label="SubItem 5"/>
      <SubItem label="SubItem 5"/>
      </MenuItem>
      </root>
        • 1. Re: Going a bit crazy...
          ringx007 Level 1
          my english is poor , so try replace the function as

          var vxml1:XML = new XML(lab);
          var vxml2:XML = new XML(lab);
          var vxml3:XML = new XML(lab);
          // add the new panel to the menu list of open panels
          menuData.MenuItem[0].appendChild(vxml1);
          menuData.MenuItem[1].appendChild(vxml2);
          menuData.MenuItem[2].appendChild(vxml3);



          /**
          i think this is because you add one object three times
          */
          • 2. Re: Going a bit crazy...
            aceinc Level 1
            ringx007,

            Thanks, that works. Can anyone tell me why?

            I changed my code to what is below and it works properly, why didn't the first version work? it is adding a new version of the node to the xml, why does it care that it added the same thing elsewhere?

            While this in fact should help me because I really didn't want to add the same item to multiple menus, it doesn't make sense to me that it should behave this way.

            Anyway, here is the code that worked;

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application
            xmlns:mx=" http://www.adobe.com/2006/mxml"
            layout="absolute">
            <mx:XML id="menuData" format="e4x">
            <root>
            <MenuItem label="Menu1">
            <SubItem label="SubItem1"/>
            </MenuItem>
            <MenuItem label="Menu2">
            <SubItem label="SubItem2"/>
            </MenuItem>
            <MenuItem label="Menu3">
            <SubItem label="SubItem3"/>
            </MenuItem>
            </root>
            </mx:XML>
            <mx:Script>
            <![CDATA[
            [Bindable] public var counter:int = 3;
            // [Bindable] public var menuData:XML = menuXML;
            private function addMenuItem():void
            {
            trace("---------------------Before-------------------------");
            trace(menuData);
            counter += 1;
            var lab:String = '<SubItem label="SubItem ' + counter + '" />';
            var vxmla:XML = new XML(lab);
            counter += 1;
            var lab:String = '<SubItem label="SubItem ' + counter + '" />';
            var vxmlb:XML = new XML(lab);
            counter += 1;
            var lab:String = '<SubItem label="SubItem ' + counter + '" />';
            var vxmlc:XML = new XML(lab);
            // add the new panel to the menu list of open panels
            menuData.MenuItem[0].appendChild(vxmla);
            menuData.MenuItem[1].appendChild(vxmlb);
            menuData.MenuItem[2].appendChild(vxmlc);
            trace("---------------------After-------------------------");
            trace(menuData);
            menu1.dataProvider = menuData;
            }
            ]]>
            </mx:Script>
            <mx:MenuBar x="36" y="15" width="510" dataProvider="{menuData}" id="menu1" showRoot="false" labelField="@label"/>
            <mx:Canvas
            width="100%" height="53" bottom="0"
            backgroundColor="#efefef"
            borderSides="top" borderThickness="3" borderColor="#666666" borderStyle="solid">
            <mx:Button right="10" verticalCenter="0" label="Add Menu Item" click="this.addMenuItem()"/>
            </mx:Canvas>
            </mx:Application>


            Thanks again for the help,

            Paul