简体   繁体   English

Backbone.js系列

[英]Backbone.js collection

i'm a beginner to backbone.js我是 backbone.js 的初学者

why would you want to use a COLLECTION?为什么要使用 COLLECTION?

Can't you just have a view associated with each model?您不能只拥有与每个 model 关联的视图吗?

Also, does an event bond to a collection of model apply to all models?此外,与 model 集合的事件绑定是否适用于所有型号?

When you are operating on large datasets it is very much necessary to have a mechanism to perform operations on whole datasets or subsets.当您对大型数据集进行操作时,非常有必要拥有一种对整个数据集或子集执行操作的机制。 In Backbone.js Collection class provides this mechanism.在 Backbone.js Collection class 中提供了这种机制。 A few very frequent usages of Collection may be outlined as follows: Collection 的一些非常常见的用法可以概括如下:

  • Suppose you are fetching the results of a query from a database.假设您正在从数据库中获取查询结果。 Now you can create a model corresponding to the structure of record, define a collection, create a collection instance, define the url (and if required customize the ajax request) and then give a call to fetch.现在您可以创建一个对应于记录结构的 model,定义一个集合,创建一个集合实例,定义 url(如果需要,可以自定义 Z2705A83A5A0659CCE34583972637EDA.5Z 请求) This is more advantageous to having an array of models: firstly it is more convenient than to iterate through the parsed json data structure and create a model from each record object.这对于拥有一组模型更有利:首先,它比遍历解析的 json 数据结构并从每条记录 object 中创建 model 更方便。 Secondly the entire collection can be updated at once to the server.其次,整个集合可以立即更新到服务器。 As pointed out by Mario, it makes working with related records very convenient.正如马里奥所指出的,它使处理相关记录非常方便。

  • Secondly, Collection class provides you a base class which you can easily auxiliate with your functions to provide custom filtering and sorting capabilities.其次,Collection class 为您提供了一个基础 class,您可以轻松地使用您的功能辅助它以提供自定义过滤和排序功能。

  • You can have Views eg.你可以有视图,例如。 data grids, trees etc to represent the collection.数据网格、树等来表示集合。 It is convenient to code and also think in terms of a view representing a collection.编写代码很方便,也可以根据表示集合的视图进行思考。

You use them when you have nested models, see http://documentcloud.github.com/backbone/#FAQ-nested当您有嵌套模型时使用它们,请参阅http://documentcloud.github.com/backbone/#FAQ-nested

I guess you could have a view associated with each model but not necessarily.我想你可以有一个与每个 model 相关联的视图,但不一定。 Not sure about the last part, maybe run a quick test.不确定最后一部分,也许运行一个快速测试。

You could use a collection is to represent a one-to-many relationship.您可以使用集合来表示一对多关系。 This is type of thing is perfect when your backend datastore is a document database (having embedded documents).当您的后端数据存储是文档数据库(具有嵌入式文档)时,这种类型的东西是完美的。

You can also use a collection to allow for iteration over your models.您还可以使用集合来允许对模型进行迭代。

Since a model can only belong to a single collection it almost makes sense at times to treat a collection like a table in the database.由于model 只能属于单个集合,因此有时将集合视为数据库中的表几乎是有意义的。 In the database I have a people table.在数据库中,我有一个 people 表。 In my javascript I have a people collection.在我的 javascript 中,我有一个人物收藏。

(The reason a model can only belong to a single collection is that upon adding a model to a collection the collection stores a reference to itself directly on the model. Adding a model to a second collection would overwrite the model's reference to the first collection.) (The reason a model can only belong to a single collection is that upon adding a model to a collection the collection stores a reference to itself directly on the model. Adding a model to a second collection would overwrite the model's reference to the first collection. )

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

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