简体   繁体   English

Backbone.js - View如何找到这个集合?

[英]Backbone.js - How is the View finding this Collection?

I'm going through a tutorial to learn how to use Backbone.js and I'm having a hard time understanding how Backbone views are "seeing" the collection. 我正在通过一个教程来学习如何使用Backbone.js,我很难理解Backbone视图如何“看到”该集合。

Below is the View code, and underneath that is the Collection code. 下面是View代码,下面是Collection代码。

I can see that the variable $albums is being assigned to the particular class '.albums' that is within the element, but I don't get how this code is referencing 'this.collection'. 我可以看到变量$ albums被分配给元素中的特定类'.albums',但是我不知道这个代码是如何引用'this.collection'的。

Both the view and the collection are being extended from standard Backbone.View and Backbone.Collection classes. 视图和集合都是从标准的Backbone.View和Backbone.Collection类扩展而来的。 Just from looking at it, I can't see how they even know each other exist. 仅仅从看它,我看不出他们甚至知道彼此存在。 I'm assuming that the word 'this' refers to this particular instance of LibraryView. 我假设'this'这个词指的是LibraryView的这个特定实例。

I guess this is my primary question: 我想这是我的主要问题:

How is it that the code collection = this.collection is able to see the external collection? 代码collection = this.collection如何能够看到外部集合?

// A wrapper view to display each album in Library
    window.LibraryView = Backbone.View.extend({         
        tagName: 'section',
        className: 'library',

        initialize: function() {
            _.bindAll(this, 'render');
            this.template = _.template($('#library-template').html());
            this.collection.bind('reset', this.render);
        },

        render: function() {            
            var $albums,
                collection = this.collection;           

            $(this.el).html(this.template({}));
            $albums = this.$('.albums');
            collection.each(function(album) {
                var view = new LibraryAlbumView({
                    model: album,
                    collection: collection
                });
                $albums.append(view.render().el);
            });
            return this;
        }

    });

Here is the Albums collection: 这是专辑集合:

// Albums Collection
    window.Albums = Backbone.Collection.extend({
        model: Album,
        url: '/albums'
    })

EDIT: 编辑:


I think I found it thanks to the help here: 我想我找到了感谢这里的帮助:

There was another piece of code creating a library variable and assigning it to a new albums collection: 还有另一段代码创建了一个库变量并将其分配给一个新的专辑集合:

window.library = new Albums();

Also, in the Router there is an initialize statement that passes in the 'library' variable: 此外,在路由器中有一个初始化语句,它传入'library'变量:

initialize: function() {
            this.libraryView = new LibraryView({
                collection: window.library
            });

Now it seems to make more sense. 现在它似乎更有意义。 :) :)

Just posting this in case someone else is as confused as I was. 只是张贴这个以防万一其他人像我一样困惑。

A collection would have to be passed to the LibraryView constructor. 必须将集合传递给LibraryView构造函数。 For example, 例如,

myLibrary = new LibraryView({
  collection: new Albums()
})

However, an important bit of magic happens here. 然而,这里有一点重要的魔力。 Everything passed to a View constructor ends up in the View's options property. 传递给View构造函数的所有内容都以View的options属性结束。 A select number of properties though, get copied over on to the view itself. 但是,选定数量的属性会被复制到视图本身。 So you can say this.collection instead of this.options.collection . 所以你可以说this.collection而不是this.options.collection

Those special properties are: 这些特殊属性是:

'model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName' 'model','collection','el','id','attributes','className','tagName'

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

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