Index < ArrayEventArgs

This indicates where the change in an ObservableArray has occurred. This may differ between ModificationTypes. For an Add and Insert, this Index represents where the element was placed. For Remove, this indicates where the element was removed. For Swap, Index is the index of the first element involved in the swap. For Move, Index is where the element was moved to. Here is an example:

var array1 = [];
var array2 = new ObservableArray();
array2.OnChange.Add(function(e){
    switch(e.ModificationType){
        case ModificationType.Add:
            for(var i = 0; i < e.Objects.length; i++){
                array1.Insert(e.Index + i, e.Objects[i]);
            }
            break;

        case ModificationType.Remove:
        case ModificationType.Clear:
            for(var i = 0; i < e.Objects.length; i++){
                array1.Remove(e.Objects[i]);
            }
            break;

        case ModificationType.Swap:
            array1.Swap(e.Index, e.Index2);
            break;

        case ModificationType.Move:
            array1.Move(e.Objects[0], e.Index);
            break;

        case ModificationType.Update:
            array1.Clear();
            for(var i = 0; i < e.Objects.length; i++){
                array1.Add(e.Objects[i]);
            }
            break;
    }
});

 

This very contrived example shows how to make sure array1 constantly has the same elements in it as array2.

Last edited Mar 26, 2011 at 5:56 PM by dahrkdaiz, version 9

Comments

No comments yet.