2 Replies Latest reply on May 10, 2009 10:29 PM by ntsiii

    Assistance in comparing 2 Objects

    addytoe85

      I have got a problem... I am currently trying to compare 2 Objects together and checking upon their IDs and after that is done, it will then update the quantity of it... Below is my coding and hopefully, it will share some light on what I am trying to achieve...

       

      [Bindable]
      private var shopCartXML:XML = new XML(<product></product>);

       

      [Bindable]
      private var shopCartData:XMLListCollection;

       

      private function addCart():void
      {
                     
                      var itemUpdate:XMLList = shopCartXML.product.(@category==musicTree.selectedItem.@category);
                      //var itemUpdate2:XMLList = shopCartXML.product.@id == musicTree.selectedItem.@id);
                      var testUpdate:Object = musicTree.selectedItem.@id;
                     
                     for  each (var item:Object in shopCartXML)
                      {
                         
                         // 0 returns true, 1 and -1 for differences
                         var testItem:int = ObjectUtil.compare(item.product.@id, testUpdate, 0);
                          trace (testItem);
                         
                          if ((testItem)  && (itemUpdate.length() > 0))
                          {
                              itemUpdate.sing_quantity = Number(itemUpdate.sing_quantity) + 1;
                          }
                          else
                          {

                              var newData:XML =
                                                      <product id={musicTree.selectedItem.@id} category={musicTree.selectedItem.@category}>
                                                         
                                                          <name>
                                                              {musicTree.selectedItem.@name}
                                                          </name>
                                                         
                                                          <category>
                                                              {musicTree.selectedItem.@category}
                                                          </category>
                                                         
                                                          <sing_price>
                                                              {musicTree.selectedItem.@sing_price}
                                                          </sing_price>
                                                         
                                                          <sing_quantity>
                                                              {musicTree.selectedItem.@sing_quantity}
                                                          </sing_quantity>
                                                         
                                                      </product>;

                               shopCartXML.appendChild(newData);
                              // We create the XMLListCollection as this will bring alot of functionalities like binding of data or update upon new changes
                              shopCartData =  new XMLListCollection(shopCartXML.children());           
                          }
                      }
                  }

       

      Basically, I am trying to do is that after the the click of the button, it will come to this addCart function and within this function, it would firstly create a shopCartXML of XML and update the details into the shopCartData and it would be displayed into the data grid. But the real problem with this is that, I would want to validate on the product's id and its category. Because, at the moment, it will only validate on the category but not on the ID. Thus, when I have got 2 items of the same category but different IDs, it will only increase on the quantity of the 1st item, which I have added but it will not add the 2nd item to the data grid and it will only increase on the quantity of the 1st item....

       

      I been working on this for 2 nights and still I have not managed to come up with a solution to solve this problem... Any ideas on where I am going wrong?? I tried my hands on ObjectUtil.compare and for each in loop but still I am not able to compare the shopCartXML ID and the Tree control selectedItem ID...

        • 1. Re: Assistance in comparing 2 Objects
          Michael Borbor Level 4

          Hi there, what is your issue?

           

          Sincerely,

           

          Michael

           

          El 10/05/2009, a las 21:20, addytoe85 <forums@adobe.com> escribió:

           

          >

          I have got a problem... I am currently trying to compare 2 Objects 

          together and checking upon their IDs and after that is done, it will 

          then update the quantity of it... Below is my coding and hopefully, 

          it will share some light on what I am trying to achieve...

          >

          private var shopCartXML:XML = new XML(<product></product>);

          >

          private var shopCartData:XMLListCollection;

          >

          private function addCart():void

          {

          >

                          var itemUpdate:XMLList = shopCartXML.product.

          (@category==musicTree.selectedItem.@category);

                          //var itemUpdate2:XMLList = shopCartXML.product.@id 

          == musicTree.selectedItem.@id);

                          var testUpdate:Object = musicTree.selectedItem.@id;

          >

                          for  each (var item:Object in shopCartXML)

                          {

          >

                              // 0 returns true, 1 and -1 for differences

                              var testItem:int = 

          ObjectUtil.compare(item.product.@id, testUpdate, 0);

                              trace (testItem);

          >

                              if ((testItem)  && (itemUpdate.length() > 0))

                              {

                                  itemUpdate.sing_quantity = 

          Number(itemUpdate.sing_quantity) + 1;

                              }

                              else

                              {

          >

          var newData:XML =

                                                          <product 

          id={musicTree.selectedItem.@id} 

          category={musicTree.selectedItem.@category}>

          >

                                                              <name>

                                                                    

          {musicTree.selectedItem.@name}

                                                              </name>

          >

                                                              <category>

                                                                    

          {musicTree.selectedItem.@category}

                                                              </category>

          >

                                                              <sing_price>

                                                                    

          {musicTree.selectedItem.@sing_price}

                                                              </sing_price>

          >

          • 2. Re: Assistance in comparing 2 Objects
            ntsiii Level 3

            I'd advise just manually comparing the property values.  Reading the doc on ObjectUtil.compare, I do not think it will do what you want.

             

            Tracy