【问题标题】:Difference between splice and remove in winjs listView to remove an itemwinjs listView中splice和remove的区别来移除一个item
【发布时间】:2014-01-24 03:36:03
【问题描述】:

在winjs中从listView中删除一个项目,这两种方法有什么区别?

方法一

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();

方法二

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

【问题讨论】:

    标签: windows-8 winjs windows-8.1


    【解决方案1】:

    它们本质上是一样的,因为方法1的remove方法实际上是使用方法2实现的。在方法1的remove调用上设置断点,然后在Visual Studio中查看方法实现:

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

    其中 _list 是您从 itemDataSource.list 获得的内容。

    真正的区别在于方法 1 具有 beginEdits 和 endEdits,因此如果您一次修改多个项目,则需要使用该变体。这将提高性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-16
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多