淘汰赛JS绑定和Ajax需要更长的时间来加载 [英] Knockout JS binding and ajax taking longer to load
本文介绍了淘汰赛JS绑定和Ajax需要更长的时间来加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下数据进行KO映射
I am trying to use below data for KO mapping
我尝试使用document.ready中的ajax成功事件对其进行初始化
I try to initialise it using ajax success event in document.ready
var organisationData = data.d; var orgObject = { organisationsData: organisationData };
var newviewmodel = ko.viewmodel.fromModel(orgObject);
问题是ajax需要花费更长的时间来加载,然后弹出JavaScript错误,表明viewmodel不存在.
The problem is ajax is taking much longer time to load and then the javascript error pops up saying the viewmodel doesnt exist.
所以
-
一旦ajax成功加载,我如何告诉Knockout重新绑定
How can I tell Knockout to rebind once the ajax has successfully loaded
当我从ajax获取的数据为空时,有没有办法告诉KO不要绑定/隐藏表?
Is there anyway I can tell KO not to bind/ hide the table when the data I got from ajax is empty?
推荐答案
我做了一个小提琴,用计时器替换了服务器调用.
I made a fiddle in which I replaced the server call with a timer.
JS:
var vm = {
items: ko.observableArray()
};
ko.applyBindings(vm);
setTimeout(function(){
var rawData =[
{name:'item1'},
{name:'item2'},
];
ko.mapping.fromJS(rawData, {}, vm.items);
// to ensure that name is an observable
var item1Name = vm.items()[0].name();
console.log(item1Name);
},1500);
查看:
<table>
<tbody data-bind="foreach : items">
<tr>
<td data-bind="text : name"></td>
</tr>
</tbody>
</table>
<span data-bind="visible : items().length == 0">Loading...</span>
希望对您有帮助.
这篇关于淘汰赛JS绑定和Ajax需要更长的时间来加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文