我们有以下场景:
One select conected to one observable property Countries, and countryId
Another child select conected to StateProvinces and stateProvinceId observable property
A compute generating the dependency between the first and second property (Countris and StateProvinces).
We click Load and simulate load a entity from the server, and update the selected values: countryId and stateProvinceId. The real coding uses the mapping plugin.
为了更清楚,请参阅工作 jsfiddle http://jsfiddle.net/sPTHm代码,然后单击“加载”。
当我们在第48行模拟州/省选择的异步ajax请求时,我们开始遇到麻烦。当这个请求是异步时,加载不起作用,只是因为stateProvinceId是在加载StateProvinces之前设置的,并且它将被重置为空。请在此处查看具有 StateProvinces 异步请求的模拟:http://jsfiddle.net/sPTHm/1/ ,然后单击“加载”。
我们已经尝试过:节流、计算可观察配方、deferEvaluation 和大量搜索。在knockoutjs中,理想的方法是什么?
最佳答案
我会尝试使用jQuery deferred concept链接您的ajax请求
return $.when(self.dataSource.getRtuUserData(rtuId))
.then(function (rtuUserData)
{
self.RtuUserData = ko.mapping.fromJS(rtuUserData);
return self.dataSource.getRtuStatus(rtuId);
})
.then(function (rtuStatus)
{
self.RtuStatus = ko.mapping.fromJS(rtuStatus);
return self.dataSource.getRtuLocation(rtuId);
})
dateSource 对象上的每个方法都包装一个 ajax 调用:
self.getRtuLinks = function (rtuId)
{
return $.ajax(
{
url: urlRoot + '/rtus/' + rtuId + '/links',
dataType: 'json',
cache: false,
type: 'GET'
});
}
这样,您就可以同步加载两组数据,这应该可以解决您的问题。
关于javascript - knockout 中级联主从和加载记录。 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14478199/