如何告诉jquerymobile推迟执行mobileinit直到加载DOM?
我正在尝试使用模板&创建页面。 JSON。我的问题是JSON加载时间太长而jquerymobile已经完成了mobileinit事件!
示例代码:
$(document).bind("mobileinit", function(){
console.log("mobileinit");
// get JSON & set template... should wait until this is done!
getData();
//SWIPE SWIPE
$('div[data-role="page"]').live("swipeleft", function(){
var nextpage = $(this).next('div[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, 'slide');
}
});
$('div[data-role="page"]').live("swiperight", function(){
var prevpage = $(this).prev('div[data-role="page"]');
// swipe using id of next page if exists
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, 'slide', true);
}
});
console.log("mobileinit done");
});
答案 0 :(得分:1)
假设getData()
函数包含$.ajax
调用,则需要将init代码的重置置于success
的{{1}}函数内{ {1}}致电
答案 1 :(得分:1)
您可以使用jQuery.Deferred()来完成此任务。
// Let's assume that getData returns a deferred.
function getData() {
return $.get('your/request/url');
});
$(document).bind("mobileinit", function() {
// Now you can use the deferred returned from the getData function.
getData().done(function() {
//SWIPE SWIPE
$('div[data-role="page"]').live("swipeleft", function() {
var nextpage = $(this).next('div[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, 'slide');
}
});
$('div[data-role="page"]').live("swiperight", function() {
var prevpage = $(this).prev('div[data-role="page"]');
// swipe using id of next page if exists
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, 'slide', true);
}
});
});
}