1

我正在关注以下教程:

http://arturalib.com/hello-backbonejs/docs/3.html

这是我坚持的一段代码:

 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();
    },

我很难理解的代码行是:

this.collection.bind('add', this.appendItem);

我知道下划线中有一个绑定方法,但我认为这不是同一个绑定函数。

你能解释一下上面的行是什么,我可以在哪里阅读更多关于它的信息?

4

2 回答 2

2

在backbonejs中,一个集合可以触发事件。例子:

this.collection.trigger('myEvent');

此外,您还可以将集合绑定到某些事件。例子:

this.collection.bind('myEvent', function() { ... });
于 2013-05-23T16:55:12.057 回答
0
  1. Backbone.Collection.bind() 方法来自 Backbone.Events。请注意,Backbone.Collection 混合了 Backbone.Events 的所有方法(就像所有其他的主干.js 对象,包括 Backbone 本身)。

  2. Backbone.Events.bind() 是 Backbone.Events.on() 的别名。

于 2014-07-23T14:07:56.537 回答