在我的淘汰代码中,我首先更新视图模型,然后根据该更新调用重载API。但我希望我的视图会在API调用完成后更新如何做到
代码就像这样
for (var i = 0; i < self.AppEntityErrorRequestAttributes().length; i++)
self.AppEntityActionRequestAttributes.push(self.AppEntityErrorRequestAttributes()[i]);
// Dont want to update view
var validationStatus = FilterValidation(self);
//debugger;
//var filterData = self.AppEntityErrorRequestAttributes();
//filterData = ko.toJSON(self.AppEntityErrorRequestAttributes());
if (validationStatus == false) {
toastr.error("You cannot save filter with a blank key", 'Sorry!');
}
else {
ajaxRequest("POST", baseUrl + 'api/' + controller + "save/", self)
.done(function (data, textStatus, jqXHR) {
$(location).attr('href', baseUrl + controller + "?entityId=" + entityId);
for (var i = 0; i < self.AppEntityErrorRequestAttributes().length; i++)
self.AppEntityActionRequestAttributes.pop(self.AppEntityErrorRequestAttributes()[i]);
// Now want to update view
});
}
答案 0 :(得分:0)
你应该使用subscribe
// Here's my data model
var ViewModel = function(first, last) {
var self = this;
var ajaxFinished = ko.observable(false);
$.ajax({
dataType: "json",
url: 'https://httpbin.org/get',
data: '',
success: function() {
ajaxFinished(true);
},
fail: function() {
alert('fail');
},
error: function() {
alert('error');
}
});
ajaxFinished.subscribe(function() {
//do something
alert('ajax finished');
});
};
ko.applyBindings(new ViewModel()); // This makes Knockout get to work
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>