我有几个 View ,其渲染结果取决于同一个集合,然后是其他模型或集合:
例子:
View1 -> model1, commonCollection
View2 -> collection2, commonCollection
View3 -> model3, commonCollection
现在,为了解决这个问题,我为每个 View 创建了 commonCollection.fetch ……在呈现页面之前,我等待每个模型/集合为每个 View 准备好
如 this answer 中所述.
但由于 commonCollection 的结果是相同的,我想缓存结果以将其用于其他 View 。
如何在不使用全局变量的情况下缓存 commonCollection 的结果?我正在使用 RequireJs
最佳答案
难道你不能只覆盖公共(public)集合中的 fetch
来进行缓存吗?
Backbone.Collection.extend({
...
fetch: function(options) {
if (alreadyUpdated) {
// do nothing
} else {
return Backbone.Collection.prototype.fetch.apply(this, arguments);
}
}
...
});
这是一个更完整的例子的 fiddle : http://jsfiddle.net/4A8Wu/2/
关于javascript - 如何使用 requireJs 缓存获取调用的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690343/