Backbone:集合事件绑定器

时间:2012-09-20 18:55:22

标签: backbone.js

这是一个5部分的backbone.js hello world教程/应用程序。 https://web.archive.org/web/20180317062059/http://arturadib.com/hello-backbonejs/docs/3.html在第3部分中,作者说明了如何使用集合和模型来存储数据以及如何将更改与视图绑定。

我理解其中的大部分内容,除了这一行

this.collection.bind('add', this.appendItem); // collection event binder

这个'bind'只是绑定上下文,还是作为'event'运行,只要添加了模型,就会调用appendItem?

我问,因为在ListView的render方法中,它显式调用了appendItem方法,所以为什么它绑定到'add'

_(this.collection.models).each(function(item){ // in case collection is not empty
    self.appendItem(item);
  }, this);

有人可以解释一下代码是如何工作的。我查看了文档,但找不到以这种方式使用的绑定的解释。

完整代码

(function($){
¶
Item class: The atomic part of our Model. A model is basically a Javascript object, i.e. key-value pairs, with some helper functions to handle event triggering, persistence, etc.

  var Item = Backbone.Model.extend({
    defaults: {
      part1: 'hello',
      part2: 'world'
    }
  });      

¶
List class: A collection of Items. Basically an array of Model objects with some helper functions.

  var List = Backbone.Collection.extend({
    model: Item
  });

  var ListView = Backbone.View.extend({
    el: $('body'),
    events: {
      'click button#add': 'addItem'
    },
¶
initialize() now instantiates a Collection, and binds its add event to own method appendItem. (Recall that Backbone doesn't offer a separate Controller for bindings...).

    initialize: function(){
      _.bindAll(this, 'render', 'addItem', 'appendItem'); // remember: every function that uses 'this' as the current object should be in here

      this.collection = new List();
      this.collection.bind('add', this.appendItem); // collection event binder

      this.counter = 0;
      this.render();      
    },
    render: function(){
¶
Save reference to this so it can be accessed from within the scope of the callback below

      var self = this;      
      $(this.el).append("<button id='add'>Add list item</button>");
      $(this.el).append("<ul></ul>");
      _(this.collection.models).each(function(item){ // in case collection is not empty
        self.appendItem(item);
      }, this);
    },
¶
addItem() now deals solely with models/collections. View updates are delegated to the add event listener appendItem() below.

    addItem: function(){
      this.counter++;
      var item = new Item();
      item.set({
        part2: item.get('part2') + this.counter // modify item defaults
      });
      this.collection.add(item); // add item to collection; view is updated via event 'add'
    },
¶
appendItem() is triggered by the collection event add, and handles the visual update.

    appendItem: function(item){
      $('ul', this.el).append("<li>"+item.get('part1')+" "+item.get('part2')+"</li>");
    }
  });

  var listView = new ListView();
})(jQuery);

1 个答案:

答案 0 :(得分:3)

假设您的收藏品已有10个型号。然后你把它传递给你的视图。你打电话给render(),它会触发appendItem()或不是this.collection.on('add', this.appendItem, this)的循环。你的观点很高兴。

然后您将模型添加到您的收藏中。 (模型11)

{{1}}不是重新渲染整个事物,而是执行将单个项目视图添加到现有视图列表的功能。

这可能就是为什么它绑定到渲染中包含的添加事件AND作为循环。一个循环遍历现有集合以在开始时生成视图。一个人负责在视图初始化和第一次渲染之后添加的任何新模型。