简体   繁体   English

我怎么知道我的馆藏已经有使用Backbone.JS的数据?

[英]How do I know my collection already has data using Backbone.JS?

I am developing a site using javascript framework BACKBONE.JS. 我正在使用javascript框架BACKBONE.JS开发网站。 In my site, There is one Category Selection drop down. 在我的网站上,有一个“类别选择”下拉列表。 Using Backbone collection fetch, I have rendered my category drop down successfully. 使用Backbone集合获取,我成功地将类别下拉列表渲染了。 In my header i have three horizontal menu[image shown below]. 在标题中,我有三个水平菜单[如下图所示]。 User click of the menu(Page navigation is done using backbone routers). 用户单击菜单(使用骨干路由器完成页面导航)。 My main content of the day will change. 我这一天的主要内容将会改变。 The user can filter the content based on the category. 用户可以根据类别过滤内容。 My category filter drop down option will not change frequently. 我的类别过滤器下拉选项不会经常更改。

水平菜单

My Router: 我的路由器:

dealapp.AppRouter = Backbone.Router.extend({
    routes: {
        "": "home",
        "all/mobile": "mobile",
        "all/descktop": "displayAllVoucher"
    },
    home: function () {},
    mobile: function () {},
    desktop: function () {}
});

Success Case 成功案例

I am loading my site Using " http://www.Site1.com/ ". 我正在使用“ http://www.Site1.com/ ”加载我的网站。 The function home will get a call and do the listed action. 函数home将获得呼叫并执行列出的操作。 If i am navigating to some other tab(mobile/desktop), my category drop down displaying.[ Note : i am fetching my category from the server in the home function] 如果我导航到其他选项卡(移动/桌面),则会显示我的类别下拉列表。[注意:我正在从home功能中的服务器中获取我的类别]

scenario 脚本

I am loading my site using " http://www.Site1.com/#all/deal " directly. 我正在直接使用“ http://www.Site1.com/#all/deal ”加载我的网站。 In this case my category drop down is not rendering , i am getting an empty drop down. 在这种情况下,我的类别下拉列表没有呈现,我得到了一个空的下拉列表。 I know that i haven't added my category fetch in the other two functions mobile and desktop. 我知道我还没有在手机和台式机的其他两个功能中添加类别获取。 If i include the category fetch in mobile and desktop function each time then my category fetch call goes to server and fetches data from server. 如果我每次都在移动和桌面功能中包括类别获取,那么我的类别获取调用将转到服务器并从服务器获取数据。

My doubt 我的疑问

How do i know if my collection already has data? 我怎么知道我的收藏中是否已有数据? I want to reuse the already downloaded data. 我想重用已经下载的数据。 If data not available in the local storage then i need to fetch it from the server. 如果数据在本地存储中不可用,那么我需要从服务器获取数据。

You can override fetch on the collection. 您可以覆盖集合上的提取。 Fetch returns a deferred object, you can store this on the collection itself. Fetch返回一个延迟的对象,您可以将其存储在集合本身上。 If the deferred is null you will call the prototype fetch. 如果deferred为null,则将调用原型访存。 The advantage is that in your code you always call fetch, and if the collection has data you return the cached data. 这样做的好处是,在代码中您总是调用fetch,并且如果集合中有数据,则返回缓存的数据。

fetch : function(options) {
  if(this.deferred){
    return this.deferred;
  }
  this.deferred = Backbone.Collection.prototype.fetch.call(this, options);
  return this.deferred;
}

This specific problem was dealt with by others and a few plugins can be found. 其他人处理了这个特定的问题,并且可以找到一些插件。

The one I am currently using with success is the Thorax framework that adds a few things over Backbone. 我目前成功使用的是Thorax框架,它在Backbone上增加了一些功能。

For Model and Collection they added isPopulated() and isEmpty() method as can be seen here in their documentation . 对于ModelCollection他们添加了isPopulated()isEmpty()方法,如其文档所示

They will tell you if there is data in the collection or not. 他们会告诉您集合中是否有数据。 If you don't want to use the entire framework, just copying the code from their Git repository here , would do. 如果您不想使用整个框架,只需从此处Git存储库复制代码即可。

In a few words they solve the problem by overriding fetch to set a property called _fetched to true when the data are fetched. 简而言之,它们通过重写fetch来解决此问题,以在提取数据时将名为_fetched的属性设置为true。


Another way would be to cache the data. 另一种方法是缓存数据。 Most of the time this is a good idea, but this depends. 在大多数情况下,这是个好主意,但这要视情况而定。 In your scenario it could be a good idea to cache it in a localStorage. 在您的方案中,最好将其缓存在localStorage中。

A plugin I found that seems to do it's job is Backbone fetch cache . 我发现一个似乎可以完成工作的插件是Backbone fetch cache

Description: 描述:

This plugin intercepts calls to fetch and stores the results in a cache object (Backbone.fetchCache._cache). 该插件拦截对提取的调用,并将结果存储在缓存对象(Backbone.fetchCache._cache)中。 If fetch is called with { cache: true } in the options and the URL has already been cached the AJAX call will be skipped. 如果在选项中使用{cache:true}调用了fetch并且URL已被缓存,则AJAX调用将被跳过。


Yet another version is mentioned in this answer: Caching collections in backbone.js? 在此答案中还提到了另一个版本:是否在ribs.js中缓存集合?

As the answerer there said, you could do it similar to this: 正如回答者所说,您可以执行以下操作:

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. 然后,您将获得完全相同的实例,而无需再次获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Backbone.js将过滤的集合转换为JSON? - How do I convert a filtered collection to JSON with Backbone.js? Backbone.js-如何将集合保存到localStorage? - Backbone.js - How do I save a collection to localStorage? Backbone.js:我正在使用Web服务调用来使用Backbone.js获取集合对象,如何在html中显示集合响应对象数据? - Backbone.js: I am using web service call to get collection object using Backbone.js, How to display the collection response object data in html? 如何仅使用Javascript在Backbone.js中引导集合 - How do I bootstrap a collection in Backbone.js using Javascript only Backbone.js视图如何知道其集合何时更改? - How does a Backbone.js View know when its collection has changed? 如何使用pubsub和backbone.js聚合事件数据? - How do I aggregate event data using pubsub with backbone.js? Backbone.js - 如何创建具有可绑定属性的集合 - Backbone.js - How to create a collection that has bindable properties Backbone.js我应该在何时何地获取收集数据? - Backbone.js Where and When should I fetch my collection data? 我在哪里将代码引导集合到belimber.js? - Where do I put the code to bootstrap a collection into backbone.js? Backbone.js我想做model.fetch并将数据反映回源集合中 - Backbone.js I want to do model.fetch and have the data reflected back in the source collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM