View comments | RSS feed

Using collection change notifications

The IList and ICollectionView interfaces include the itemUpdated() method that notifies the collection that the underlying data has changed and ensures that the collection view of the data is up to date. This method takes the item that was modified, the property in the item that was updated, and its old and new values as parameters.

The ICollectionView interface also provides the enableAutoUpdate() and disableAutoUpdate() methods, which enable and disable the automatic updating of the collection view when the underlying data provider changes.

Subtopics

Using the itemUpdated method
Disabling and enabling automatic updating

Using the itemUpdated method

Use the itemUpdated() method to notify the collection of changes to a data provider object if the object does not implement the IEventDispatcher interface; in this case the object is not monitorable. Flash and Flex Objects and other basic data types do not implement this interface. Therefore, You must use the itemUpdated method to update the collection when you modify the properties of a data provider such as an Array or through the display object.

You can also use the itemUpdated() method if you must use an Array, rather than a collection, as a control's data provider. Then the component wraps the Array in a collection wrapper. The wrapper needs to be manually notified of any changes made to the underlying Array data object, and you can use the itemUpdated() for that notification.

You do not have to use the itemUpdated() method if you add or remove items directly in a collection or use any of the ICollectionView or IList methods to modify the collection.

Also, specifying the [Bindable] metadata tag above a class definition, or above a variable declaration within the class ensures that the class implements the IEventDispatcher interface, and causes the class to dispatch propertyChange events. If you specify the [Bindable] tag above the class declaration, the class dispatches propertyChange events for all properties; if you mark only specific properties as [Bindable], the class dispatches events for only those properties. The collection listens for the propertyChange events. Therefore, if you have a collection called myCollection that consists of instances of a class that has a [Bindable] myVariable variable, an expression such as myCollection.getItemAt(0).myVariable="myText" causes the item to dispatch an event and you do not have to use the itemUpdated() method. (For more information on the [Bindable] metadata tag and its use, see Binding Data.)

The most common use for t he itemUpdate() method is to notify a collection of changes to a custom class data source that you cannot make [Bindable] or modify to implement the IEventDispatcher interface. The following schematic example shows how you could use the itemUpdated() method in such a circumstance:

Assume you have a class that you do not control or edit that looks like the following:

public class ClassICantEdit {
    public var field1:String;
    public var field2:String;
}

You have an ArrayCollection that uses these object, such as the following, which you populate with classICantEdit objects.

public var myCollection:ArrayCollection = new ArrayCollection();

You have DataGrid control such as the following

<mx:DataGrid dataProvider="{myCollection}"/>

When you update a field in the myCollection ArrayCollection, as follows, the DataGrid does not automatically update.

myCollection.getItemAt(0).field1="someOtherValue";

To update the DataGrid control, you must use the collection's itemUpdated() method:

myCollection.itemUpdated(collectionOfThoseClasses.getItemAt(0));

Disabling and enabling automatic updating

The ICollectionView disableAutoUpdate() method prevents events that represent changes to the underlying data from being broadcast by the view. It also prevents the ICollectionView from updating as a result of these changes.

Use this method to prevent the ICollectionView, and therefore the control that uses it as a DataProvider, from showing intermediate changes in a set of multiple changes. The DataGrid class, for example, uses the disableAutoUpdate() method to prevent updates to the ICollectionView object while a specific item is selected. When the item is no longer selected, the DataGrid calls the enableAutoUpdate() method. Doing this ensures that, if a DataGrid uses a sorted collection view, items that you edit do not jump around while you're editing.

You can also use the disableAutoUpdate() method to optimize performance in cases where multiple items in a collection are being edited at once. By disabling the auto update until all changes are made, a control like DataGrid can receive an update event as a single batch instead of reacting to multiple events.

Revised 9/20/2006: Fixed method names in sample code.

The following code snippet shows the use of the disableAutoUpdate() and enableAutoUpdate() methods:

var obj:myObject = myCollection.getItemAt(0);
myCollection.disableAutoUpdate();
obj.prop1 = 'foo';
obj.prop2 = 'bar';
myCollection.enableAutoUpdate();

Flex 2

Comments


juan.mendez said on Sep 19, 2006 at 8:30 PM :
change myCollection.disableAutoUpdates() for myCollection.disableAutoUpdate()

change myCollection.enableAutoUpdate() for myCollection.enableAutoUpdates()

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/docs/00000508.html