在缓存Backbone.js的收藏品?缓存、收藏品、Backbone、js

2023-09-10 16:00:05 作者:没有金箍棒就不要穿超短裙

什么是确保我的收藏保持高速缓存,因此只被取一次?最好的办法

我应该实现某种形式的缓存层?我应该分享集合变量任何需要的地方?我能相信jQuerys AJAX的设置? ( $ ajaxSetup({缓存:真});

基本集合,因为它看起来现在:

  theCollection = Backbone.Collection.extend({
    型号:theModel,
    网址:source.json
});

如果(typeof运算MyCollection的===未定义){
    VAR MyCollection的=新theCollection; //仅允许它创建一次
}
 

解决方案

我会实现你的情况了一种集合经理:

  VAR经理=(函数(){

  VAR构造= {
    例如':ExampleCollection
  };
  变种集合= {};

  返回 {
    getCollection:函数(名){
      如果(!收藏[名]){
        VAR收集=新的构造函数[名]();
        collection.fetch();
        集合[名] =集合;
      }
      返回集合[名]
    }
  }
})();
 

下面的经理负责实例化集合和获取它们。当你调用:

  VAR exampleCollection = manager.getCollection(示例);
 
js如何获取缓存

你得到的数据是已经获取例如收集的一个实例。当你需要这个集合一次,你可以再次调用该方法。然后,您将得到无需再次获取它完全一样的实例。

这只是一个很简单的管理器示例,并有很多附加功能,可以实现和提高。

我会强烈建议不要在一个较低的水平处理这个问题(如的$就传输层)。如果你这样做,你会prevent您的收藏从得到牵强多次,但你最终不得不用相同的ID漂浮你的应用程序不同的模型实例。每一个集合类实例将创建自己的模型。

在一个CouchApp我目前工作的,我也认为有必要在不同的集合prevent重复的模型实例(不同的数据库的视图可以返回相同的模型数据)。这已经解决了有一个单独的集合中的管理器,它跟踪的所有型号已经加载到应用程序。

最后,但你可能会考虑实施的集合刷新方法或管理者将处理来自服务器的更新集合并非最不重要的。如果你这样做的方法获取你的整个集合reseted因此,所有车型被破坏并重新创建。这是不好的,如果你有从这个集合中的应用程序中引用其他地方的模型(像通常那样)。这些实例是过时和重复你的应用程序,然后。刷新方法检查羯羊与输入ID实例已经present在了短路电流收集。如果是这样,他们被更新,否则它们被添加

What would be the best way to ensure that my collection stays cached and thereby only gets fetched once?

Should I implement some sort of cache layer? Should I share the Collection variable to wherever it is needed? Can I trust jQuerys AJAX setting? ($.ajaxSetup ({ cache: true });)

The basic collection as it looks right now:

theCollection = Backbone.Collection.extend({
    model: theModel,
    url: "source.json"
});

if (typeof myCollection === 'undefined') {
    var myCollection = new theCollection; // Only allow it to be created once
}

解决方案

I would implement a sort of collection manager in your case:

var manager = (function(){

  var constructors = {
    'example': ExampleCollection
  };
  var collections = {};

  return {
    getCollection: function(name) {
      if(!collections[name]) {
        var collection = new constructors[name]();
        collection.fetch();
        collections[name] = collection;
      }
      return collections[name];
    }
  }
})();

Here the manager is responsible for instantiating collections and fetching them. When you call:

var exampleCollection = manager.getCollection('example');

you get an instance of example collection with data being already fetched. Whenever you need this collection again you can call the method again. You will then get the exact same instance with no need to fetch it again.

This is just a very simple manager example, and there are a lot of additional features you can implement and enhance.

I would strongly advise not to handle this issue on a lower level (eg. the transport layer of $.ajax). If you do that, you would prevent your collection from getting fetched multiple times, but you end up having different model instances with the same id floating around your application. Every collection instance would create it's own models.

In a CouchApp I am currently working on, I also found it necessary to prevent duplicate model instances in different collections (different db views can return the same model data). This has been solved by having a separate collection in the manager, which keeps track of all models already loaded into the application.

Last but not least you might consider implementing a refresh method in your collections or the manager that will handle updating the collection from the server. If you do this with the fetch method your whole collection is reseted so all models are destroyed and then recreated. This is bad if you have models from this collection referenced somewhere else in the app (as you typically do). Those instances are outdated and duplicated in your app then. The refresh method checks wether instances with the incoming id are already present in the curent collection. If so they are updated, otherwise they are added.