DataSet.modelChanged

Availability

Flash Player 7.

Edition

Flash MX Professional 2004.

Description

Usage 1:

var listenerObject:Object = new Object();
listenerObject.modelChanged = function (eventObj:Object):Void {
    // ...
};
dataSetInstance.addEventListener("modelChanged", listenerObject);

Usage 2:

on (modelChanged) {
    // ...
}

Description

Event; broadcast when the collection changes in some way--for example, when items are removed or added to the collection, when the value of an item's property changes, or when the collection is filtered or sorted.

The event object (eventObj) contains the following properties:

target The DataSet object that generated the event.

type The string "iteratorScrolled".

firstItem The index (number) of the first item in the collection that was affected by the change.

lastItem The index (number) of the last item in the collection that was affected by the change (equals firstItem if only one item was affected).

fieldName A string that contains the name of the field being affected. This property is undefined unless the change was made to a property of the DataSet object.

eventName A string that describes the change that took place. This can be one of the following values:

String value

Description

"addItems"

A series of items has been added.

"filterModel"

The model has been filtered, and the view needs refreshing (reset scroll position).

"removeItems"

A series of items has been deleted.

"schemaLoaded"

The fields definition of the data provider has been declared.

"sort"

The data has been sorted.

"updateAll"

The entire view needs refreshing, excluding scroll position.

"updateColumn"

An entire field's definition in the data provider needs refreshing.

"updateField"

A field in an item has been changed and needs refreshing.

"updateItems"

A series of items needs refreshing.

Example

In the following example, the modelChanged event gets dispatched whenever an item is added or removed from the data set:

my_ds.addEventListener("modelChanged", onModelChanged);
function onModelChanged(evt_obj:Object):Void {
    trace("[event =" + evt_obj.eventName + "] the data set now has " + evt_obj.target.items.length + " items.");
}
my_ds.addItem({name:"Apples", price:14});
my_ds.addItem({name:"Bananas", price:8});
my_ds.removeItemAt(0);

In the following example, a Delete Item button is disabled if the items have been removed from the collection and the target DataSet object has no more items:

my_ds.addEventListener("modelChanged", onModelChanged);
function onModelChanged(evt_obj:Object):Void {
    trace("model changed, DataSet now has " + evt_obj.target.items.length + " items");
}
// Disable events for the data set.
my_ds.disableEvents();

my_ds.addItem({name:"Apples", price:14});
my_ds.addItem({name:"Bananas", price:8});

trace("Before:");
traceItems();

my_ds.last();
while (my_ds.hasPrevious()) {
   my_ds.price *= 0.5; // Everything's 50% off!
   my_ds.previous();
}

trace("After:");
traceItems();

// Tell the dataset it's time to update the controls now.
my_ds.enableEvents();

function traceItems(label:String):Void {
    for (var i:Number = 0; i < my_ds.items.length; i++) {
        trace("\t" + my_ds.items[i].name + " - $" + my_ds.items[i].price);
    }
    trace("");
}

See also

DataSet.isEmpty()


Flash CS3