Knockout Mapping re-rendering everything
I'm using the Knockout mapping plug-in to refresh the UI with JSON
retrieved from the server every 3 seconds. The UI consists of some nested
foreach bindings. However, it seems that everything in all the foreach
bindings are getting completely deleted and re-rendered with every
refresh, even when nothing has changed.
function ViewModel() {
var self = this;
this.refreshUrl = $("[data-view=edit]").data("url");
this.refresh = function(callback) {
$.get(self.refreshUrl, function(data) {
ko.mapping.fromJSON(data, {}, self);
if (typeof callback == "function") {
callback.call(self);
}
});
}
this.addedQuestion = function() {
// Gets called for every question every refresh by afterRender
// Never gets called at all by afterAdd
}
};
var refreshing = false, handler;
window.viewModel = new ViewModel();
//Initialize the UI after initial AJAX is completed
viewModel.refresh(function() {
ko.applyBindings(this);
$(document).on("click", ".add-question", function() {
if (!refreshing) {
handler = setInterval(viewModel.refresh, 3000);
refreshing = true;
}
});
});
Does anyone see anything glaringly wrong with this?
No comments:
Post a Comment