2015-12-08 91 views
0

好的,所以我需要一些洞察與History.js和jQuery的工作。處理狀態變化jQuery和History.js

我已經建立並運作(只是不像您期望的那樣)。

我有如下:

$(function() { 

var History = window.History; 
if (!History.enabled) { 
     return false; 
} 

// Capture all the links to push their url to the history stack and trigger the StateChange Event 
$('.ajax-link').click(function(e) { 
    e.preventDefault(); 

    var url = this.href; //Tells us which page to load 
    var id = $(this).data('passid'); //Pass ID -- the ID in which to save in our state object 
     e.preventDefault(); 
     console.log('url: '+url+' id:'+id); 

     History.pushState({ 'passid' : id }, $(this).text(), url); 
}); 

History.Adapter.bind(window, 'statechange', function() { 
    console.log('state changed'); 
    var State = History.getState(), 
     id = State.data.editid; //the ID passed, if available 

    $.get(State.url, 
     { id: State.data.passid }, 
     function(response) { 

     $('#subContent').fadeOut(200, function(){ 
      var newContent = $(response).find('#subContent').html(); 
      $('#subContent').html(newContent); 

      var scripts = $('script'); 
      scripts.each(function(i) { 
      jQuery.globalEval($(this).text()); 
     }); 


      $('#subContent').fadeIn(200); 
     }); 
    }); 
}); 
}); //end dom ready 

它可以作爲你只要更改URL,傳遞的ID,改變內容的期望。我的問題是這樣的: 如果我在瀏覽器上向前/向後按幾次subContent部分將基本上淡入/淡出多次。

任何洞察力是讚賞。謝謝

============================================= ======

編輯:問題是在我打電話給我的所有<script>和Eval他們在每個狀態變化。通過添加類=「無重載」的歷史管理腳本標籤我是能夠做到:

var scripts = $('script').not('.no-reload'); 

這擺脫了問題,現在按預期工作。圖一我會在這裏留下來,以防其他人遇到同樣的問題。

+1

我們不會在SO上的標題中做「(已解決)」。如果您能夠回答自己的問題,您可以添加答案並將其標記爲已接受,或者您可以刪除該問題。 – j08691

+0

好的,我試圖編輯它並添加一個答案,但我不知道我應該回答我自己的主題與答案。原帖中沒有辦法做到這一點嗎? – Imperialized

+0

http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question和http://meta.stackexchange.com/questions/17463/can-i-answer-my-own -questions偶數如果-I-知道最答案前方的問 – j08691

回答

0

問題出在我打電話給我的所有<script>,並在每個狀態變化上評估它們。通過添加類=「無重載」的歷史管理腳本標籤我是能夠做到:

var scripts = $('script').not('.no-reload'); 

這擺脫了問題,現在按預期工作。圖一我會在這裏留下來,以防其他人遇到同樣的問題。