简体   繁体   English

Backbone listenTo回调参数

[英]Backbone listenTo callback arguments

I'm new to Backbone and I'm confused by a snippet of the sample todo app code: http://backbonejs.org/docs/todos.html 我是Backbone的新手,我对示例待办事项应用代码的片段感到困惑: http//backbonejs.org/docs/todos.html

On the AppView object there's a function called addOne that takes an argument: AppView对象上有一个名为addOne的函数,它接受一个参数:

addOne: function(todo) {
  var view = new TodoView({model: todo});
  this.$("#todo-list").append(view.render().el);
},

This function is called whenever the add event is triggered on the Todos model. 只要在Todos模型上触发add事件,就会调用此函数。 There doesn't seem to be anything in here that tells the listener that it needs to pass in a todo argument to the addOne function: 这里似乎没有任何东西告诉监听器它需要将一个todo参数传递给addOne函数:

this.listenTo(Todos, 'add', this.addOne);

When the event gets triggered and this.addOne is called, how does Backbone know to provide addOne with it's todo argument, since it doesn't seem to be specified in the listenTo invocation? 当事件被触发并且this.addOne ,Backbone如何知道为addOne提供它的todo参数,因为它似乎没有在listenTo调用中指定?

Backbone.Collection.add & Backbone.Collection.create will trigger the add event passing model, this, options as arguments like this. Backbone.Collection.addBackbone.Collection.create将触发add事件传递model, this, options作为这样的参数。

.trigger('add', model, this, options);

this.addOne is then passed these arguments when called by listenTo . 然后this.addOnelistenTo调用时传递这些参数。

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

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