sup*_*ool 1 asynchronous knockout.js
我点击了一个按钮,我打电话给:
视图模型:
self.MyArray = ko.observableArray();
self.remove = function(c) {
ko.utils.arrayForEach(c.sData(), function(ser) {
if (ser.Check() == true) {
self.MyArray.push(service);
count++;
}
});
if (count) {
$.ajax({
url: "/api/LoadCustomer/?reason=" + reason,
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(self.MyArray),
success: function(data) {
ko.utils.arrayForEach(self.MyArray(), function(removedata) {
c.sData.remove(removedata);
});
}
});
self.MyArray([]);
}
};
Run Code Online (Sandbox Code Playgroud)
如果我包含async:false在我的ajax调用中我得到一切正常,但如果我没有包括async(它的默认属性是真的)我不知道在我的成功函数ajax调用self.MyArray()仍然,Empty但它不是如此,如果我保持虚假(尴尬) .
有时我担心,如果我async:true在加载到可观察数组(OnLoad)时有一系列的ajax调用,则可能存在轻微错位数据的可能性.
帮帮我理解
你的observable数组MyArray为空的原因有时是因为这行代码self.MyArray([]);在ajax调用之前执行完毕.这怎么样 -
if (count) {
$.ajax({
url: "/api/LoadCustomer/?reason=" + reason,
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(self.MyArray),
success: function (data) {
ko.utils.arrayForEach(self.MyArray(), function (removedata) {
c.sData.remove(removedata);
});
},
complete: function() { // complete fires after success and error callbacks
self.MyArray([]); // empty the array no matter what happens (error or success)
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |