Replacing item in observableArray

前端 未结 3 1365
半阙折子戏
半阙折子戏 2021-02-01 02:47

I\'m trying to replace all of the contents of an item in an observableArray with new content.

var oldLocation = ko.utils.arrayFirst(self.locations()         


        
3条回答
  •  眼角桃花
    2021-02-01 03:29

    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.

    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.


    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:

    pop, push, reverse, shift, sort, splice, unshift, slice (readonly).

    Knockout provides these additional methods which should be documented here (currently v3.5.1):

    remove, removeAll, destroy, destroyAll, indexOf, replace, sorted, reversed

提交回复
热议问题