javascript 替换 observableArray 中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15440810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing item in observableArray
提问by bflemi3
I'm trying to replace all of the contents of an item in an observableArray
with new content.
我正在尝试observableArray
用新内容替换一个项目的所有内容。
var oldLocation = ko.utils.arrayFirst(self.locations(), function (item) {
return item.id == value.id;
});
self.locations.replace(self.locations.indexOf(oldLocation), new location(value));
self.locations.valueHasMutated();
I've also tried
我也试过
self.locations[self.locations.indexOf(location)] = new fizi.ko.models.location(value);
But nothing is working. The index is being properly retrieved but the update of the item isn't happening.
但没有任何效果。索引正在被正确检索,但项目的更新没有发生。
回答by Jeff Mercado
The replace function accepts two parameters, the item you want to replace and the new item you want to replace it with. You are passing in the index in place of the item to replace so it doesn't work.
replace 函数接受两个参数,要替换的项目和要替换的新项目。您正在传递索引代替要替换的项目,因此它不起作用。
The replace call should be:
替换调用应该是:
self.locations.replace(oldLocation, new location(value));
On a side note, you shouldn't need the valueHasMutated()
call there, it will get invoked by the replace()
call.
附带说明一下,您不应该在valueHasMutated()
那里需要调用,它会被调用replace()
调用。
Side note, many of the native Array functions are available for observable arrays. They are forwarded to the underlying array value triggering notifications of mutations as needed. These include:
旁注,许多本机 Array 函数可用于可观察数组。它们被转发到底层数组值,根据需要触发突变通知。这些包括:
pop
, push
, reverse
, shift
, sort
, splice
, unshift
, slice
(readonly).
pop
, push
, reverse
, shift
, sort
, splice
, unshift
, slice
(只读)。
Knockout provides these additional methods which should be documented here(currently v3.5.1):
Knockout 提供了这些额外的方法,应在此处记录(当前为 v3.5.1):
remove
, removeAll
, destroy
, destroyAll
, indexOf
, replace
, sorted
, reversed
remove
, removeAll
, destroy
, destroyAll
, indexOf
, replace
, sorted
,reversed
回答by JotaBe
I simply want to mention an alternative way to do it:
我只想提一种替代方法:
self.locations.splice(
self.locations.indexOf(location), // Index of the 1st element to remove
1, // Number of elements to remote at this index
new fizi.ko.models.location(value) // A param for each element to add at the index
);
Knockout includes splice
in its documentation, but doesn't include replace
: Knockout Obervable Arrays Docs. However, if you look at the source code you'll see that both functions are implemented (at least in KO 3.0, I don't know if replace
was missing in previous versions).
Knockout 包括splice
在其文档中,但不包括replace
:Knockout Obervable Arrays Docs。但是,如果您查看源代码,您会发现这两个功能都已实现(至少在 KO 3.0 中,我不知道replace
以前的版本中是否缺少)。
回答by Paul Manzotti
I'm not aware of a replace method in JavaScript for arrays, or in Knockout. Am I missing something?
我不知道 JavaScript 中用于数组或 Knockout 的替换方法。我错过了什么吗?
If you want to use your second method, then you need to access locations as an observable:
如果要使用第二种方法,则需要将位置作为可观察对象访问:
self.locations()[self.locations.indexOf(location)] = new fizi.ko.models.location(value);
self.locations.valueHasMutated();
though you don't when using indexOf, as there is a Knockout version of that for observable arrays.
尽管您在使用 indexOf 时不会,因为有一个用于可观察数组的 Knockout 版本。