In KnockoutJS, what is the correct way to update a monitored JSON dataset every time I execute an AJAX command?
Right now I'm quenching an array using something like viewmodel.items ([]) and then re-populating it with JSON data from the server. If you are not using the KnockoutJS mapping plugin (which may be the only way to do this), what is the correct way?
My server logic is going to send the same data every time, so I can’t just iterate over and move items to an array if I don't want duplicates.
//// Adding how I do it today ////
I'm not sure why I am doing this, but this is exactly how I initially understood how to update. So basically, as I said, I get the JSON data, then pass it something like this:
_model.addIncident = function (json) { var checked = json.UserTouches > 0 ? true : false; _model.incidents.push({ id: ko.observable(json.IncidentIDString), lastTouchId: ko.observable(json.UserLastTouchIDString), weight: ko.observable(json.Weight), title: ko.observable(json.Title), checked: ko.observable(checked), createdOn: ko.observable(json.IncidentCreatedOn), servicename: ko.observable(json.Servicename), inEdit: ko.observable(false), incidentHistory: ko.observableArray(), matchScore: ko.observable() }); };
for each node in the JSON array. As you can see, I have some custom watch elements that are collected with each data transfer. Perhaps this is the wrong way, but so far it has worked perfectly.
source share