Search code examples
windows-8winjswindows-8.1

Difference between splice and remove in winjs listView to remove an item


Removing an item from a listView in winjs, what is the difference between those 2 methods?

Method 1

var _dataSource = myList.itemDataSource;
//Start the sequence of edits
_dataSource.beginEdits();

//remove the firstitem
_dataSource.remove(_dataSource.itemFromIndex(0)._value.key);

//Ends the batch of edits
_dataSource.endEdits();

Method 2

myList.itemDataSource.list.splice(0, 1); //remove the first in the list  

Solution

  • They're essentially identical, because Method 1's remove method is actually implemented using Method 2. Set a breakpoint on Method 1's remove call, and then looked at the method implementation in Visual Studio:

    function remove(key) {
        var index = this._list.indexOfKey(key);
        if (index === -1) {
            return errors.noLongerMeaningful;
        }
        this._list.splice(index, 1);
        return;
    }
    

    Where _list is what you get from itemDataSource.list.

    The difference really lies in the fact that Method 1 has beginEdits and endEdits, so you'd want to use that variant if you were modifying more than one item at a time. That will improve performance.