Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 集合上的主干js绑定方法_Backbone.js - Fatal编程技术网

Backbone.js 集合上的主干js绑定方法

Backbone.js 集合上的主干js绑定方法,backbone.js,Backbone.js,我将在以下位置阅读教程: 这是我一直坚持的一段代码: 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.collec

我将在以下位置阅读教程:

这是我一直坚持的一段代码:

 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)

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


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

在backbonejs中,集合可以触发事件。例如:

this.collection.trigger('myEvent');
this.collection.bind('myEvent', function() { ... });
此外,还可以将集合绑定到某些事件。例如:

this.collection.trigger('myEvent');
this.collection.bind('myEvent', function() { ... });
  • Backbone.Collection.bind()方法来自Backbone.Events。请注意,Backbone.Collection混合了Backbone.Events的所有方法(与包括主干本身在内的所有其他Backbone.js对象一样)

  • Backbone.Events.bind()是Backbone.Events.on()的别名