backbone.js获取缓存的结果

dag*_*da1 41 backbone.js

我在以下backbone.js控制器的索引操作中使用fetch:

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.");
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

问题是结果正在缓存.它不会获取数据库更改.反正告诉backbone.js不要缓存结果吗?

我知道我可以覆盖集合的url并添加时间戳,但我正在寻找比这更清洁的东西.

Jul*_*ien 57

这通常是IE上的问题,而骨干与它无关.你必须转到jQuery ajax调用并查看doc.Backbone使用jquery ajax作为其同步方法.您可以执行以下操作以强制所有浏览器调用ajax:

$.ajaxSetup({ cache: false });
Run Code Online (Sandbox Code Playgroud)

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


Mik*_*Dev 37

@ Julien的建议会起作用,但每个AJAX请求都会到达服务器,并且不会从缓存中检索任何内容.

$.ajaxSetup({ cache: false });
Run Code Online (Sandbox Code Playgroud)

还有另一种方法.您可以将"cache:false"作为fetch中的选项传递(请参阅下面的代码).好处是具有"cache:false"的fetch将始终命中服务器,而其他fetch可以从缓存中检索数据.我正在编写的应用程序,异步访问数据和内容.有时我希望从缓存中检索项目,有时我想要点击服务器.

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

jQuery.ajax选项也可以直接作为fetch选项传递,以便获取分页集合的特定页面:Documents.fetch({data:{page:3}})

您也可以覆盖集合的fetch方法,类似于此代码.

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

.

下面我在fetch中添加了"cache:false"

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.");
            }
        });
Run Code Online (Sandbox Code Playgroud)

  • 经测试,在Backbone 0.9.10中,options参数为null.我最终这样做:`fetch:function(options){options = options || {}; options.cache = false; 返回Backbone.Collection.prototype.fetch.call(this,options);}` (8认同)