5 Replies Latest reply on Jan 18, 2010 11:17 PM by stephgravity

    Editing a current item of an arrayCollection

    John Hall Level 4

      Seemed pretty intuitive but I guess I've not done it before in this way. I have a collection of divisions (sports divisions in a league) in an arrayCollection. To edit info for any division, I use a dropdown list that has a dataProvider of the array collection using the name field as the label field. That works fine the first time but when I drop down the list for the second time, it actually updates the current division with the new division info that's being instantiated.

       

      That sounds difficult to visualize.

       

      I have a form called DivisionForm with fields populated by [Bindable] model.currentDivision

       

      I have a manager that acts as the model which injects the values for divisions and currentDivision into the form.

       

      When I change the dropdown item, it dispatches an event to the manager which says, in short, currentDivision = divisions.getItemAt[dropDown.selectedItemIndex]

       

      So that's not really how it works but conceptually that's what's happening. But when I try to change out the currentDivision, it actually updates by reference the old item.

       

      So, let's say I  start with an arraycollection of

       

      softball

      baseball

      tball

       

      and the currentDivision is softball in the form. When I dispatch the event, the manager gets the message fine and the function is (simplified)

       

      public function changeCurrentDivision(e:DivisionEvent):void {
                  var division:Division = e.division;

                 currentDivision = e.division
      }

       

      so now I have

      baseball

      baseball

      tball

       

      I understand the concept of both pointing to the same arrayCollection but I can not figure out how to simply say set the form elements with the currentDivsion being baseball, don't update the existing currentItem reference in the arrayCollection. I've tried making currentDivision a deep copy of the original object within the arrayCollection but no go. What simple principle am I missing here? The only ways I've been successful are way too verbose and obviously stupid so I'm looking for the right way ;-)

        • 1. Re: Editing a current item of an arrayCollection
          John Hall Level 4

          Well, I did get it to work by making a deep copy of the division item I passed in but I'm still wondering if I'm missing something simple so educate me.

           

          thx

          • 2. Re: Editing a current item of an arrayCollection
            stephgravity Level 1

            Hi I have the same problem as you, but I don't understand your solution, can you please explain it more detailed?

             

            Thanks,

            Stephanie

            • 3. Re: Editing a current item of an arrayCollection
              John Hall Level 4

              My solution doesn't feel ideal but maybe it will elicit suggestions regarding a better way to handle it. Essentially, I have an array collection of custom objects related to a sports league. I bind one of those, say division[2] to a construct called currentDivision and that division objects to fields in a form and had bindings to specific properties like {currentDivision.name} . I could update the current item but then when I would change the dropDownList to select another division, and through a manager had the value for currentDivision bind to, let's say, division[4] as the new currentDivision, division[2] would be populated by division[4]. Obviously, the reference was a pointer that was telling flex to change currentDivision [2] to the values for division[4] rather than start fresh with a new object. My hunch is there's a way to properly edit that object using the dp.source but I haven't hit on the right search terms yet for my friend Google. So instead I decided to make a copy of the division[2] to populate the editing form so that it was divorced from the actualy dataCollection. To do that I had to make a deep copy of the division object, which involved a couple of things. I created a little class because I knew I was going to be doing this a lot that was a copier.

               

                  public class DeepCopy {

               

                      import mx.utils.ObjectUtil;

               

                      public function DeepCopy() {
                      }

               

                      public static function copyLO(item:Object):Object {
                          var myCopy:Object=ObjectUtil.copy(item);
                          return myCopy;
                      }
                  }

               

              but this copies all the properties as generic objects so I also had to modify my division object with some metadata. So the division class looked something like:

               

              package com.cactusware.model {
                  import mx.utils.UIDUtil;
                  import mx.collections.ArrayCollection;

               

                  [Bindable]
                  [RemoteClass(alias="com.cactusware.model.Division")]
                  public class Division {
                      private var _Guid:String;
                      private var _name:String;
                      private var _shortCode:String;
                      private var _longCode:String;
                      private var _notes:String;
                     
                      private var _customCodes:ArrayCollection;

               

                      public function Division() {
                          Guid = UIDUtil.createUID();
                      }

               

                      public function get Guid():String {
                          return _Guid;
                      }

               

              ..... and so forth.

               

              Now in my app I could make a copy that was pretty divorced from the actual dataprovider.

               

              var newDivision:Division = DeepCopy.copyLO(currentDivision) as Division;

               

              At least those are the principles summarized.

               

              Now hopefully one of the gurus will tell us how to do this without going through all the gyrations, assuming they understand the problem we're having.

               

              John

              • 4. Re: Editing a current item of an arrayCollection
                archemedia Level 4

                I think I understand your problem and can help but I'd really like a simplified working version which isolates the problem.

                 

                d

                • 5. Re: Editing a current item of an arrayCollection
                  stephgravity Level 1

                  Thanks! I will go for the moment in the direction you proposed (in your solution).

                  Stephanie