【发布时间】:2014-04-03 09:07:51
【问题描述】:
我正在寻找一种方法来删除可观察数组中给定索引之后的所有元素。
执行此操作的 for 循环效率低下,因为一次删除一个元素会在每次删除时触发更改通知。有什么开箱即用的吗?
【问题讨论】:
我正在寻找一种方法来删除可观察数组中给定索引之后的所有元素。
执行此操作的 for 循环效率低下,因为一次删除一个元素会在每次删除时触发更改通知。有什么开箱即用的吗?
【问题讨论】:
试试这个(注意 HowMany 是可选的,如果你不指定它,StartIndex 之后的所有项目都会被删除):
myObservableArray.splice(StartIndex, HowMany)
如果你需要删除具有某些属性的项目,你可以传递一个返回布尔值的函数给knockout的remove函数,例如:
myObservableArray.remove(function(item) { return item.property > YourValue })
+ 引自淘汰文档:LINK
Normally, an observableArray notifies its subscribers immediately, as soon as it’s
changed. But if an observableArray is changed repeatedly or triggers expensive updates,
you may get better performance by limiting or delaying change notifications. This is
accomplished using the rateLimit extender like this:
// Ensure it notifies about changes no more than once per 50-millisecond period
myViewModel.myObservableArray.extend({ rateLimit: 50 });
【讨论】: