从骨干型号VS集合中获取数据 [英] Backbone fetching data from model vs collection

查看:104
本文介绍了从骨干型号VS集合中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的骨干工程,它获取所有书籍的详细信息,并显示在用户界面。我取出由模型中的所有书籍的详细信息。不是在所有使用收集这样的事情

I created a simple backbone project, where it fetches all books details and show it in 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
        })
    }
});

同样的效果。我不明白为什么要在我的情况下使用集合。请大家帮我理解这个概念与我的例子,为什么我要在这里使用的集合。我Google了很多,能找到最好的答案。

The same effect. I don't understand why to use Collections in my case. 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.

感谢

推荐答案

想象一下,你有两个2路:

Imagine that you have two 2 routes:

/books
/books/:id

现在获取一个特定的书,你可以发送到 /电子书/请求:ID 路线,其中:ID ID 这本书的。

Now for getting a specific book you can send a request to /book/:id route, where :id is the id of the book.

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.

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

骨干遵循同样的原则。型号为单本图书。收集了许多书。当你使用一个集合,骨干会为每本书一个模型。使用模型的多个项目是错误的。

Backbone follows the same principle. Model for a single book. Collection for many books. When you use a collection, Backbone creates one Model for each book. Using a Model for more than one item is wrong.

您说骨干创建每本书一个模型。在哪一步它创建?

You said "Backbone creates one Model for each book.". at what step it creates?

它创建于同步事件,即模型时获取的所有项目的请求完成。

It creates the models on the sync event, i.e. when the request for getting all the items is complete.

...它怎么做帮助我。在我的情况,我总是获取所有的书籍,而不是一本书。

...how does it helps me. In my case I always fetch all books, not single book.

主干收藏总是用骨干模型。如果不设置收藏的模式属性明确,骨干网使用正常模式,即模式的集合属性应始终引用模型。

Backbone Collections always use Backbone Models. If you don't set the model property of the Collection explicitly, Backbone uses a normal Model, i.e. the model property of a Collection should always refer to a 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,

考虑一个模型作为的对象的和集合作为的对象的数组的。

这篇关于从骨干型号VS集合中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆