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.");
}
});
}
});
问题是结果被缓存。它不会获取数据库更改。有没有办法告诉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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一个解决方案是防止在服务器端使用
php 中的
HTTP 标头进行缓存,或者使用 Express 和 Coffeescript 中的 Node.js 中的类似内容来防止在服务器端进行缓存
Another solution is to prevent caching on the server side with HTTP headers
in php
or something like this in node.js with express and coffeescript
添加 'cache:false' 来获取工作!
我能够修复一个仅在不使用开发工具时出现在 IE 中的错误。
Adding 'cache:false' to fetch worked!
I was able to fix a bug that only appeared in IE when dev tools was not being used.
这通常是 IE 上的问题,backbone 与此无关。您必须转到 jQuery ajax 调用并查看文档。 Backbone 使用 jquery ajax 作为其同步方法。您可以执行以下操作来强制所有浏览器上的 ajax 调用:
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:
http://api.jquery.com/jQuery.ajaxSetup/
@Julien 的建议会起作用,但是每个 AJAX 请求都会到达服务器,并且不会从缓存中检索到任何内容。
还有另一种方法可以做到这一点。您可以将“cache: false”作为提取中的选项传递(请参阅下面的代码)。好处是具有“cache:false”的 fetch(s) 将始终命中服务器,而其他 fetch(s) 可以从缓存中检索数据。我当前正在编写的应用程序异步访问数据和内容。有时我想从缓存中检索项目,有时我想访问服务器。
http://documentcloud.github.com/backbone/#Collection-fetch
您还可以类似于此代码重写集合的 fetch 方法。
。
下面我在获取中添加了“cache: false”
@Julien's recommendation will work, but every AJAX request will hit the server and nothing will be retrieved from the cache.
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
You can also override the collection's fetch method similar to the this code.
.
Below I added "cache: false" to the fetch