backbone.js fetch results cached

后端 未结 4 1703
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 02:39

I am using fetch in the index action of the following backbone.js controller:

App.Controllers.PlanMembers = Backbone.Controller.extend({
    routes: {
        \"         


        
4条回答
  •  情话喂你
    2021-01-31 03:04

    @Julien's recommendation will work, but every AJAX request will hit the server and nothing will be retrieved from the cache.

    $.ajaxSetup({ cache: false });
    

    There is another way of doing this. You could pass "cache: false" as an option in the fetch (see code below). The benefit is that fetch(s) that have "cache:false" will always hit the server and the other fetch(s) may retrieve data from the cache. The application I’m currently writing, access data and content asynchronously. Sometimes I want items to be retrieved from cache and sometimes I want to hit the server.

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

    jQuery.ajax options can also be passed directly as fetch options, so to fetch a specific page of a paginated collection: Documents.fetch({data: {page: 3}})

    You can also override the collection's fetch method similar to the this code.

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

    .

    Below I added "cache: false" to the fetch

    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.");
                }
            });
    

提交回复
热议问题