简体   繁体   English

Backbone 从模型和集合中获取数据

[英]Backbone fetching data from model vs collection

I created a simple backbone project, where it fetches all books details and show it in UI.我创建了一个简单的主干项目,它获取所有书籍的详细信息并将其显示在 UI 中。 I am fetching all the books details from the model.我正在从模型中获取所有书籍的详细信息。 not at all using collection something like this根本不使用像这样的集合

var BookModel= Backbone.Model.extend({
    initialize: function(){
       this.fetchData();
    },

    fetchData: function(){
        this.url= "/get/all_books";

        this.fetch({
            success: success_callback,
            error: error_callback
        })
    }
});

This is working fine.这工作正常。 But why do I have to use collections ?但是为什么我必须使用集合? If I would have used collection it would be something like as follows如果我使用集合,它将如下所示

var BookModel= Backbone.Model.extend({
    defaults:{
        id:'',
        name: '',
        author: ''
    }
});


var BookCollection= Backbone.Collection.extend({
    model: BookModel,

    initialize: function(){
        this.fetchData();
    },

    fetchData: function(){
        this.url= "/get/all_books";

        this.fetch({
            success: success_callback,
            error: error_callback
        })
    }
});

The same effect.一样的效果。 I don't understand why to use Collections in my case.我不明白为什么在我的情况下使用 Collections。 please help me to understand this concept with my example why do I have to use collections here.请用我的例子帮助我理解这个概念,为什么我必须在这里使用集合。 I Googled a lot and could find the best answer.我用谷歌搜索了很多,可以找到最好的答案。

Thanks谢谢

Imagine that you have two 2 routes:假设您有两条 2 条路线:

/books
/books/:id

Now for getting a specific book you can send a request to /book/:id route, where :id is the id of the book.现在要获取特定书籍,您可以向/book/:id路由发送请求,其中:id是书籍的id

GET /books/1
< { id: 1, title: 'On the Genealogy of Morality', ... } 

Now what happens if you want to get all the books?现在如果你想得到所有的书怎么办? You send a request to /books route.您向/books路由发送请求。

GET /books
< [{ id: 1, title: '...', ... }, { id: 2, title: '...', ... }, ...]

Backbone follows the same principle. Backbone 遵循相同的原则。 Model for a single book.单本书的模型。 Collection for many books.许多书籍的收藏。 When you use a collection, Backbone creates one Model for each book.当您使用集合时,Backbone 会为每本书创建一个模型。 Using a Model for more than one item is wrong.将模型用于多个项目是错误的。

You said "Backbone creates one Model for each book.".你说“Backbone 为每本书创建一个模型。”。 at what step it creates?它在哪个步骤创建?

It creates the models on the sync event, ie when the request for getting all the items is complete.它在sync事件上创建模型,即当获取所有项目的请求完成时。

...how does it helps me. ...它对我有什么帮助。 In my case I always fetch all books, not single book.就我而言,我总是取所有的书,而不是一本书。

Backbone Collections always use Backbone Models. Backbone 集合总是使用 Backbone 模型。 If you don't set the model property of the Collection explicitly, Backbone uses a normal Model, ie the model property of a Collection should always refer to a Model.如果您没有设置model中的财产Collection明确,骨干网使用正常模式,即model集合的属性应始终参考模型。

// https://github.com/jashkenas/backbone/blob/master/backbone.js#L782

// Define the Collection's inheritable methods.
_.extend(Collection.prototype, Events, {

  // The default model for a collection is just a **Backbone.Model**.
  // This should be overridden in most cases.
  model: Model,

Consider a Model as an object and a Collection as an array of objects .将模型视为对象,将集合视为对象数组

More than anything, dividing your data into logical units (models) and grouping them into categories (collections) make it easy for you to reason about, manipulate and change your data.最重要的是,将数据划分为逻辑单元(模型)并将它们分组到类别(集合)中,使您可以轻松地推理、操作和更改数据。 Once you build something that is even just a tiny bit more complex than what you have built, this becomes a priority.一旦你构建的东西比你已经构建的稍微复杂一点,这就会成为优先事项。 The point isn't that you are getting some magic functionality that you couldn't otherwise get.关键不在于您获得了一些原本无法获得的神奇功能。 It's all javascript after all.毕竟都是javascript。 The point is that models and collections provide data-structuring that is helpful when building dynamic applications.关键是模型和集合提供了有助于构建动态应用程序的数据结构。 In fact that's the whole point of backbone and MV* in general, to provide helpful tools and abstractions.事实上,这就是主干和 MV* 的全部要点,以提供有用的工具和抽象。 Collections are a solution to a whole host of problems that you will run into later down the road, when you add even the tiniest bit of extra complexity to your app.集合是解决您以后会遇到的一系列问题的解决方案,当您向应用程序添加哪怕是最微小的额外复杂性时。

So, you ask why you have to use collections and I guess the answer that you already knew is, you don't have to use collections.所以,你问你为什么要使用集合,我猜你已经知道的是,你不必使用集合了答案。 In fact, it sounds like you don't need to use an MV* library at all.事实上,听起来您根本不需要使用 MV* 库。

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

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