if (FITUI.sessionViewModel === undefined) {
// http://stackoverflow.com/questions/10048485/how-to-clear-remove-observable-bindings-in-knockout-js
FITUI.sessionViewModel = ko.mapping.fromJS(rawData.session, mappingOptions);
ko.applyBindings(FITUI.sessionViewModel, sessionElement); // Initialize model with DOM
}
else {
// Need to clean the viewmodel and update UI....but how???
// Attemps so far:
// FITUI.sessionViewModel.mappedDestroyAll();
//for (var observableArray in FITUI.sessionViewModel)
// observableArray().removeAll();
// ko.cleanNode(sessionElement);
// cleanSessionObservableArrays(FITUI.sessionViewModel);
ko.mapping.fromJS(rawData.session, mappingOptions, FITUI.sessionViewModel); // Just update model with new data
}
--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
resetViewModel = function (viewModel) {
// Set arrays to []
for (var observableArray in viewModel) {
if (observableArray !== "__ko_mapping__" && viewModel[observableArray].removeAll) {
// console.log("RemoveAll() on ", observableArray);
viewModel[observableArray].removeAll();
}
}
}
//on new raw data from file:
resetViewModel(FITUI.sessionViewModel);
ks.mapping.fromJS(...)
This seems like it could be a common-enough use case to merit enhancing ko.mapping.fromJS()--the case where the data may not always be of the same structure.
I think it would be good for there to be an option to tell fromJS() to produce a result identical to what an initial call to mapping would have produced with the same data--pruning arrays/emptying observables for elements not present in the new data--but reusing existing observables which are likely bound to DOM elements in the display.