JabbyPanda’s travel to RIA world / 35 posts / categories / 85 comments / feed / comments feed

Hint: Always better to use ‘itemUpdated’ method instead of ’setItemAt’ when updating existing items in ArrayCollection

   When you want to update already existing data item in your ArrayCollection in ActionScript 3 with a new values for object's properties you have 2 options to select from:

  1. use itemUpdated(obj) ArrayCollection's method.
  2. use setItemAt(obj, index) ArrayCollection's method.

   ActionScript code sample:

    //Update an existing person in the ArrayCollection via setItemAt

    public function updatePersonViaSetItemAt():void {
        var currentlySelectedItem : Object = dg.selectedItem;
        currentlySelectedItem.first = firstInput.text;
        currentlySelectedItem.last = lastInput.text;
        ac.setItemAt(currentlySelectedItem, dg.selectedIndex);
    }

    // Update an existing person in the ArrayCollection via itemUpdated

    public function updatePersonViaItemUpdated():void {
        var currentlySelectedItem : Object = dg.selectedItem;
        currentlySelectedItem.first =  firstInput.text;
        currentlySelectedItem.last =  lastInput.text;
        ac.itemUpdated(currentlySelectedItem);

    }

   Which one to choose?

   I would strongly advice to always use 'itemUpdated' because of the following reasons:

   Interactive sample with "View source" option enabled to watch the difference:
http://jabbypanda.com/labs/updateArrayCollectionItem/updateArrayCollectionItem.html

You can try the following:

  1. First apply sorting to any of the DataGrid's column
  2. Select the first row in the Datagrid
  3. Rename the surname of the person to something completely new starting with a new letter
  4. Press "Update via setItemAt()" button control

Voilà, new unwanted row becomes visible in the Datagrid.

1 Comment

  1. Todd Rothe — April 17, 2009 #

    Thanks for the sample code. The adobe docs are a bit lacking.

Leave a comment

Powered by WP Hashcash