简体   繁体   English

在Backbone.js中使用模型过滤/排序集合

[英]Using a Model to Filter/Sort a Collection in Backbone.js

After several days of reading tutorials and searching The Google I have hit a wall... 经过数天的阅读教程和搜索Google,我碰壁了...

I'm creating an app using backbone.js that has an Item Model / Items Collection with various views to show the Items. 我正在使用骨架.js创建一个具有Item Model / Items Collection的应用程序,并带有各种视图来显示Items。 A portion of the app will allow users to create groups (and use existing groups) to display a subset of the Items Collection. 该应用程序的一部分将允许用户创建组(并使用现有组)以显示Items Collection的子集。

There will be a Group Model / Groups Collection with various views to show the groups and allow users to associate Items with a specific group. 将有一个带有各种视图的组模型/组集合,以显示组并允许用户将项目与特定组相关联。

The problem is that each item can belong to one, many or none of the groups and I cannot figure out how to use the Group Model to get a subset of the Items Collection based on what items have been associated with it or this is even the right way to go about it. 问题是每个项目可以属于一个,多个或不属于任何组,而我无法弄清楚如何使用组模型基于与之相关联的项目来获取项目集合的子集,甚至是正确的方法去做。

Any help would really be appreciated! 任何帮助将不胜感激!

Depending on how many models you actually have, where you will be storing them, and how they will be rendered in the UI will depend on how you go about approaching this. 根据您实际拥有的模型的数量,将它们存储在何处以及如何在UI中呈现它们将取决于您如何实现这一点。

If there are many models, which are stored in a database on a remote server, and items of groups will be rendered separately, one way would to go would be to load the items of the group on demand. 如果有很多模型,这些模型存储在远程服务器上的数据库中,并且组项目将分别呈现,那么一种方法是按需加载组项目。 Maybe something like ... 也许像...

var Item = Backbone.Model.extend({});
var ItemColleciton = Backbone.Collection.extend({
  url: '/load-items',
});

var Group = Backbone.Model.extend({
  items: new ItemCollection(),
  initialize: function() {
    // load the groups items as the group is initialized
    this.items.fetch({data:{groupID:this.id}});
  }
});

Alternatively, if you have fewer models, you may wish to load them all at once, and use filtering to populate the groups. 另外,如果模型较少,则可能希望一次加载所有模型,然后使用过滤来填充组。 Something like this ... 像这样的东西...

var Item = Backbone.Model.extend({});
var ItemColleciton = Backbone.Collection.extend({
  url: '/load-groups',
});

var Group = Backbone.Model.extend({
  initialize: function() {
    // load the groups items as a subset of the already loaded group collection
    this.items = new ItemCollection(allItems.filter(function(item) {
      return item.GroupID = this.id;
    }, this));
  }
});

// in your router init ...
var allItems = new ItemCollection();
allItems.fetch();

The above are just example approaches. 以上只是示例方法。 What I found after using backbone for a while, is that it's really open to interpretation, and can be implemented quite differently to solve different problems. 在使用骨干一段时间后,我发现它确实可以解释,并且可以以完全不同的方式解决不同的问题。

You may also wish to check out backbone relational . 您可能还希望检查主干关系 I've not used it myself, but believe it has added support for mapping relationships between models and collection. 我自己没有使用过它,但相信它增加了对模型和集合之间的映射关系的支持。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM