2012-05-21 35 views
1

我有渲染结果取决于同一个集合多个视图,然后其他型号或类别:如何使用requireJs缓存获取调用的结果?

Examaples:

View1 -> model1, commonCollection 
View2 -> collection2, commonCollection 
View3 -> model3, commonCollection 

眼下,解决了我做commonCollection.fetch每个视图的问题...并在呈现页面之前,我等待每个模型/集合都准备好了,如this answer中所解释的,每个视图 。
但由于commonCollection的结果是相同的,我想缓存结果用于其他视图。

如何在不使用全局变量的情况下缓存commonCollection的结果?我正在使用RequireJs

回答

2

难道你不能只是覆盖fetch在常见的集合做缓存?

Backbone.Collection.extend({ 
    ... 
    fetch: function(options) { 
     if (alreadyUpdated) { 
     // do nothing 
     } else { 
      return Backbone.Collection.prototype.fetch.apply(this, arguments); 
     } 
    } 
    ... 
}); 

下面是一个更完整的示例的小提琴: http://jsfiddle.net/4A8Wu/2/

+0

凡/我应该怎么定义'alreadyUpdated'产生这样的结果的记忆在我的commonCollection?谢谢 – antonjs

+0

@AntoJs我做了一个小提琴,希望能帮助你:http://jsfiddle.net/4A8Wu/2/ –