2011-12-15 4 views
0

jquerymobileにDOMがロードされるまで、mobileinitの実行を延期するように通知するにはどうすればよいですか?jsonがロードされるまでmobileinitに実行を一時停止する方法

テンプレートを使用してオンザフライでページを作成しようとしています& JSON。私の問題は、JSONの読み込みに時間がかかり、jquerymobileがmobileinitイベントを終了したことです!

例コード:getData()機能を仮定し

$(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

'getData()'のコードを表示できますか? – graphicdivine

答えて

1

success関数内のinitコードのリセットを配置する必要がありますこれを行うには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); 
      } 
     }); 
    }); 
} 
1

$.ajaxコールが含まれている、あなたはgetData()$.ajaxコール

関連する問題