2012-03-26 8 views
1

私はjqmを使って新しいアプリケーションを構築しています。 すべてのjavascriptは、別のページ(cat.html)上のindex.htmlに配置されています。カテゴリ内の投稿を表示するために無限のスクロールを使用しています。スクロールすると、15個の投稿の後に15個以上15個続きます.... cat.htmlページが訪問され、その関数が実行された関数は他のすべてのページで実行され続けます(cat.htmlにアクセスした後でのみ)。JqueryMobile - どのように私は1つのhtmlページでのみ関数を実行するのですか?

これは、実行のコードです:

は、どのように私はこのコードを使用していない他のページを教えていますか?

catページのためにあなたが scrollstopイベントハンドラとしたときに scrollstopイベントハンドラをバインド解除することができます catページ用 pagehideイベント火災をバインドすることができ pageshowイベントが発生
$('#SaloonaMobileCatPage').live('pageshow', function(event) 
{ 

$(window).bind('scrollstop', function(){ 

if ($(this).scrollTop() + $(this).height() >= ($(document).height() - 100)){ 

$.mobile.showPageLoadingMsg(); 
var id = getUrlVars()["catid"]; 
sf = sf + 15; 


$.ajax({ 
type: "GET", 
cache: false, 
url: "http://saloona.co.il/mobile/mobile_xml/read_cats_xml.php?cat_id=" + id +  "&start_from=" + sf, 
dataType: "xml", 
success: parseXmlCatsMore 
}); 

} 
});  

答えて

2

$(document).delegate('#SaloonaMobileCatPage', 'pageshow', function(event) { 

    $(window).bind('scrollstop.cat-page', function(){ 

     if ($(this).scrollTop() + $(this).height() >= ($(document).height() - 100)){ 

      $.mobile.showPageLoadingMsg(); 
      var id = getUrlVars()["catid"]; 
      sf = sf + 15; 

      $.ajax({ 
       type  : "GET", 
       cache : false, 
       url  : "http://saloona.co.il/mobile/mobile_xml/read_cats_xml.php?cat_id=" + id + "&start_from=" + sf, 
       dataType : "xml", 
       success : parseXmlCatsMore 
      }); 
     } 
    }); 
}).delegate('#SaloonaMobileCatPage', 'pagehide', function() { 
    $(window).unbind('scrollstop.cat-page'); 
}); 

お知らせ方法削除するイベントハンドラだけを追加/削除できるように、イベントタイプに名前空間を追加しました。

別のアプローチは、現在のページがcatページであるかどうかを確認するscrollstopイベントハンドラにif/thenステートメントを追加することです:私は$.mobile.activePage[0].idを使用して、現在のページのIDを取得する方法

$(document).delegate('#SaloonaMobileCatPage', 'pageshow', function(event) { 

    $(window).bind('scrollstop.cat-page', function(){ 

     if ($.mobile.activePage[0].id == 'SaloonaMobileCatPage' && $(this).scrollTop() + $(this).height() >= ($(document).height() - 100)){ 

      $.mobile.showPageLoadingMsg(); 
      var id = getUrlVars()["catid"]; 
      sf = sf + 15; 

      $.ajax({ 
       type  : "GET", 
       cache : false, 
       url  : "http://saloona.co.il/mobile/mobile_xml/read_cats_xml.php?cat_id=" + id + "&start_from=" + sf, 
       dataType : "xml", 
       success : parseXmlCatsMore 
      }); 
     } 
    }); 
}); 

お知らせとcatページのIDと照合して、ユーザーがcatページにある場合にのみif/thenステートメント内のコードが実行されるようにします。

+0

ジャスパーさん、ありがとうございました。 2番目のアプローチはトリックを行った –

関連する問題