backbone.js 获取缓存的结果

发布于 2024-11-10 12:02:30 字数 1182 浏览 0 评论 0原文

我在以下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.");
            }
        });
    }
});

问题是结果被缓存。它不会获取数据库更改。有没有办法告诉backbone.js不要缓存结果?

我知道我可以覆盖集合的 url 并附加时间戳,但我正在寻找比这更干净的东西。

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

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

The problem is that the results are being cached. It does not pick up database changes. Is there anyway to tell backbone.js not to cache the results?

I know I could override the url of the collection and append a timestamp but I am looking for something a bit cleaner than that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-11-17 12:02:31

另一个解决方案是防止在服务器端使用

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
?>

HTTP 标头进行缓存,或者使用 Express 和 Coffeescript 中的 Node.js 中的类似内容来防止在服务器端进行缓存

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

Another solution is to prevent caching on the server side with HTTP headers

in 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
?>

or something like this in node.js with express and coffeescript

res.send results,
  "Cache-Control": "no-cache, must-revalidate"
  "Expires": "Sat, 26 Jul 1997 05:00:00 GMT"
没有心的人 2024-11-17 12:02:31

添加 'cache:false' 来获取工作!

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

我能够修复一个仅在不使用开发工具时出现在 IE 中的错误。

Adding 'cache:false' to fetch worked!

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

I was able to fix a bug that only appeared in IE when dev tools was not being used.

辞旧 2024-11-17 12:02:30

这通常是 IE 上的问题,backbone 与此无关。您必须转到 jQuery ajax 调用并查看文档。 Backbone 使用 jquery ajax 作为其同步方法。您可以执行以下操作来强制所有浏览器上的 ajax 调用:

$.ajaxSetup({ cache: false });

http://api.jquery.com/jQuery.ajaxSetup/< /a>

This is a problem on IE usually and backbone has nothing to do with it. You have to go down to the jQuery ajax call and look at the doc. Backbone uses jquery ajax for its sync method. You can do something like this to force ajax call on all browsers:

$.ajaxSetup({ cache: false });

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

ま柒月 2024-11-17 12:02:30

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

$.ajaxSetup({ cache: false });

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

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

jQuery.ajax选项也可以直接作为获取选项传递,所以
获取分页集合的特定页面:
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);
     }
     ...
})

下面我在获取中添加了“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.");
            }
        });

@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.");
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文