2011-05-30 37 views
41

我使用取下面的Backbone.js的控制器的索引動作:Backbone.js的提取結果緩存

App.Controllers.PlanMembers = Backbone.Controller.extend({ 
    routes: { 
     "": "index" 
    }, 

    index: function() { 
     var planMembers = new App.Collections.PlanMembers(); 

     planMembers.fetch({ 
      success: function() { 
       var recoveryTeam = planMembers.select(function (planMember) { 
        return planMember.get("TeamMemberRole") == "RecoveryTeam"; 
       }); 

       var otherMembers = planMembers.select(function (planMember) { 
        return planMember.get("TeamMemberRole") == "Other"; 
       }); 

       new App.Views.Index({ collection: { name: "Team", members: recoveryTeam }, el: $('#recoveryTeam') }); 

       new App.Views.Index({ collection: { name: "Team", members: otherMembers }, el: $('#otherTeam') }); 
      }, 
      error: function() { 
       alert('failure'); 
       showErrorMessage("Error loading planMembers."); 
      } 
     }); 
    } 
}); 

的問題是,結果被緩存。它不會選擇數據庫更改。無論如何告訴backbone.js不要緩存結果?

我知道我可以覆蓋集合的URL並附加一個時間戳,但我正在尋找比這更乾淨的東西。

+0

同樣的問題在這裏。 – 2011-05-30 18:36:19

+2

試圖確認你在問什麼。您可以在使用瀏覽器撥打電話的封面下進行一次AJAX調用。瀏覽器沒有進行調用,因爲它遵守緩存指令?基本上,這可能不是一個backbone.js問題,因爲backbone.js沒有緩存。它始終用收到的內容替換集合中的所有模型。也許在你的問題中增加一些瀏覽器的XHR細節,並澄清你的問題。 – 2011-05-31 00:31:37

回答

57

這是IE上的問題,通常和骨幹網無關。您必須下載jQuery ajax調用並查看文檔。 Backbone使用jquery ajax作爲其同步方法。你可以做這樣的事情,迫使所有的瀏覽器的AJAX調用:

$.ajaxSetup({ cache: false }); 

http://api.jquery.com/jQuery.ajaxSetup/

+1

謝謝。簡單而直接! – 2013-10-25 22:14:19

+1

你已經救了我很多你甚至不知道的痛苦。 – 2015-02-12 21:56:20

+0

@ErikAhlswedeಠ_ಠ – pabo 2017-08-17 23:12:43

3

另一種解決方案是爲了防止緩存在服務器端使用HTTP頭

在PHP

<?php 
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 
?> 

或類似的東西在node.js中使用express和coffeescript

res.send results, 
    "Cache-Control": "no-cache, must-revalidate" 
    "Expires": "Sat, 26 Jul 1997 05:00:00 GMT" 
36

@ Julien的建議可以工作,但是每個AJAX請求都會觸發服務器,並且不會從緩存中檢索任何內容。

$.ajaxSetup({ cache: false }); 

還有一種方法可以做到這一點。您可以在提取中將「cache:false」作爲選項傳遞(請參閱下面的代碼)。好處是,具有「cache:false」的提取將始終命中服務器,而其他提取可能會從緩存中檢索數據。我正在編寫的應用程序,異步訪問數據和內容。有時我希望從緩存中檢索項目,有時候我想要訪問服務器。

http://documentcloud.github.com/backbone/#Collection-fetch

jQuery.ajax選項也可以直接傳遞作爲提取選項,所以 抓取分頁集合中的特定頁面: Documents.fetch({數據:{頁:3}} )

您也可以重寫與此代碼類似的集合的獲取方法。

var PlanMembers = Backbone.Collection.extend({ 
    ... 
    fetch: function (options) { 
     options = options || {}; 
     options.cache = false; 
     return Backbone.Collection.prototype.fetch.call(this, options); 
    } 
    ... 
}) 

下面我補充說:「緩存:假」以獲取

planMembers.fetch({ 
      cache: false, //Hit the server 
      success: function() { 
       var recoveryTeam = planMembers.select(function (planMember) { 
        return planMember.get("TeamMemberRole") == "RecoveryTeam"; 
       }); 

       var otherMembers = planMembers.select(function (planMember) { 
        return planMember.get("TeamMemberRole") == "Other"; 
       }); 

       new App.Views.Index({ collection: { name: "Team", members: recoveryTeam }, el: $('#recoveryTeam') }); 

       new App.Views.Index({ collection: { name: "Team", members: otherMembers }, el: $('#otherTeam') }); 
      }, 
      error: function() { 
       alert('failure'); 
       showErrorMessage("Error loading planMembers."); 
      } 
     }); 
+8

經過測試,在Backbone 0.9.10中options參數爲null。我結束了這樣做: 'fetch:function(options){options = options || {}; options.cache = false;返回Backbone.Collection.prototype.fetch.call(this,options);}' – 2013-03-19 12:29:04

+0

這個答案,@PålOliver的補充肯定是要走的路。 Backbone(和其他)和Require可以幫助我們避免全局依賴。明確是imo的最佳選擇。 – Hypnovirus 2015-01-07 16:02:27

0

添加「緩存:假」來獲取工作!

this.foo.fetch({ cache:false }); 

我能夠修復一個只在IE沒有使用開發工具時出現的錯誤。