I have been trying to figure this out for quite some time. I could not find anything that could solve this problem, but please correct me if I am wrong.
Problem: I have data from the JSON API that is included with the nested array / object structure. I use matching to initially populate the model with my data. To update this, I want to expand the model if new data appears, or update existing data.
As I understand it, the matching option key should do this trick for me, but I may have misunderstood the functionality of the matching options.
I reviewed the problem that will be presented in this example:
var userMapping = { key: function(item) { return ko.utils.unwrapObservable(item.id); } };
Fiddle: http://jsfiddle.net/mikaelbr/gDjA7/
As you can see, the first row inserts data. All is well. But when I try to update, it replaces the contents. The same for the third display; It replaces the content, not extends it.
Am I using this incorrectly? Should I try to expand the contents manually before using the mapping?
Change decision:
I solved this case by having a second helper array storing all current models. According to new data, I expanded this array and updated the view model to contain the accumulated elements.
During the upgrade (in my case the WebSocket message), I went in cycles on the models, changed the contents of the element in question and used the value of the HasMutated () method to notify the changed value in the Knockout library.
source share