我实际上建议您立即更新视图模型并在 ajax 调用失败时撤消该更改。它会让你的应用看起来更快——因为 90% 以上的 ajax 调用都有望成功,所以为 10% 的情况编写代码是没有意义的。
编辑:我强烈推荐这个视频。它出色地支持了这一点。
但这是如何做到的:
这一切的本质是computed
对你的拦截originalObservable
:
ko.computed({
read: function() {
return originalObservable();
},
write: function( newVal ) {
// perform ajax call
$.post( '<url>' , { data: originalObservable() } , function() {
// in your success callback
// update your observable
originalObservable( newVal );
});
// note: we get to this point without updating the originalObservable
// because this is part of the synchronous code
// where as the ajax call will, of course, be asynchronous
}
});
您可以使用许多不同的方式:
-1- 在您的视图模型上作为单独的属性并将您的编辑 html 绑定到此
function Viewmodel() {
this.original = ko.observable();
this.editable: ko.computed( /* as above */ );
}
-2- (1) 的轻微变化,但更简洁,是将计算结果放在可观察对象上作为属性:
function Viewmodel() {
this.original = ko.observable();
this.original.editable: ko.computed( /* as above */ );
}
-3- 创建一个自定义绑定,为您创建这个计算,让您的视图模型更加干净:
ko.bindingHandlers.myAjaxSave = {
init: function( element, valueAccessor ) {
var originalObservable = valueAccessor();
var editable = ko.computed({
read: function() {
return originalObservable();
},
write: function( newVal ) {
// perform ajax call
$.post( '<url>' , { data: originalObservable() } , function() {
// in your success callback
// update your observable
originalObservable( newVal );
});
// note: we get to this point without updating the originalObservable
// because this is part of the synchronous code
// where as the ajax call will, of course, be asynchronous
}
});
// this applies the binding to the HTML element
ko.applyBindingToNode( element , {
value: editable
});
}
}
您现在可以在输入 HTML 元素中使用它,例如:
<input data-bind="myAjaxSave: Person.Name" />