[英]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.add
& Backbone.Collection.create
将触发add
事件传递model, this, options
作为这样的参数。
.trigger('add', model, this, options);
this.addOne
is then passed these arguments when called by listenTo
. 然后this.addOne
在listenTo
调用时传递这些参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.