2012-09-08 20 views
2

我想用BB.js構建一個小應用程序。Backbone.js和即獲取工作只使用開發工具

當然,一切工作在FF,CHROME &歌劇,但不與IE瀏覽器。

我只是試圖使用Restful(php後端)獲取模型來獲取模型的集合。

在IE中甚至在多次刷新後都沒有發生。但是當我打開de dev工具來檢查控制檯並且我進行了刷新時,突然它就起作用了。

模型&收集

(function($) { 


//a fact model 
window.Fact = Backbone.Model.extend({ 

    defaults: { 
     factContent: '' 
    }, 


    initialize: function Fact(){ 
     console.log("just created a fact"); 

     this.url = "fact.php?fact="+this.id, 

     this.bind("error", function(model, error){ 
      console.log(error); 
     }); 
    }, 

    parse : function(resp, xhr) { 

     //new fact added 
     if(resp.type == "create") 
      this.url = "fact.php?fact="+resp.id; 

     return resp; 
    } 

}); 

//collection of models 
window.Facts = Backbone.Collection.extend({ 

    model: Fact, 

    url: "facts.php", 

    initialize: function(){ 
     console.log('fact collection created'); 
    } 
}); 


//facts view 
window.FactsCollectionView = Backbone.View.extend({ 

    el: $("#factsCollectionContainer"), 

    initialize: function(){ 
     this.template = _.template($('#factsCollectionTemplate').html()); 

     //binding 
     _.bindAll(this, 'render'); 
     this.collection.bind('change', this.render); 
     this.collection.bind('add', this.render); 
     this.collection.bind('remove', this.render); 
     this.collection.bind('reset', this.render); 

    }, 

    render: function(){ 
     var renderedContent = this.template({facts : this.collection.toJSON()}); 
     $(this.el).html(renderedContent); 
     return this; 
    } 

}); 


$(document).ready(function(){ 
    //create a fact collection and populate it 
    factsc = new Facts(); 


       //NOT WORKING IN IE (no alerts) 
       //WORKING ONLY USING DEV TOOL 
    factsc.fetch({success:function(){ 
     //create a view and show collection after fetch is done 
     factsView = new FactsCollectionView({collection:factsc}); 
     factsView.render(); 

     alert("success fetch"); 
    }, error: function(){ 
     alert("error fetch"); 
    }});  
}); 





})(jQuery); 

取返回此JSON: [{ 「ID」: 「48」, 「factContent」: 「你好」},{ 「ID」: 「47」,「factContent 「:」World「}]

回答

4

我認爲這是由IE緩存ajax調用引起的。檢查這個問題:backbone.js fetch results cached。基本上,你可以強制IE不緩存你的請求是這樣的:

factsc.fetch({ 
cache: false, 
success:function(){ /* stuff */ 
}, 
error:function() {/* error message */ 
}); 
2

我也有類似的問題。我已通過刪除來解決console.log

您也可以嘗試相同的操作。它看起來很傻,但它起作用。

謝謝。

2

我們面臨類似的問題。我們通過

+0

此解決方案對我來說最合適。 – Anks

1

這種精確的情況讓我絕對最近apopleptic。問題確實是IE緩存AJAX調用結果。但是,測試這個理論是非常困難的。您會看到,Microsoft打開調試控制檯時有助於禁用緩存。 (當我寫'有幫助'時,我的意思是它具有法律允許的最大諷刺數量。)通過這樣做,調試任何緩存問題就成爲WTF的一項練習。

步驟#1:用戶報告問題,所以你在IE中嘗試並確認問題。

步驟#2:打開調試控制檯並逐步瀏覽它,只是發現問題已經神祕消失。

步驟#3:關閉調試器並再次嘗試一次,只是發現它再次失敗。

泡沫,漂洗重複。

我的問題很複雜,因爲所涉及的網站在HTTPS中運行,不應該以任何方式,形狀或形式進行緩存。微軟甚至同意這一點:

https://msdn.microsoft.com/library/ie/dn265017%28v=vs.85%29.aspx

注意小心,但是,聲明「HTTPS頁面不會被緩存出於安全考慮。」事實上,這是事實。但是,AJAX響應似乎不符合「HTTPS頁面」的要求。

總之,禁用jQuery中的緩存。對於Backbone應用程序,將以下內容放在應用程序的init()函數的頂部應該可以實現:

$ .ajaxSetup({ cache:false });