admin管理员组文章数量:1287535
I'm new to Backbone and I'm confused by a snippet of the sample todo app code: .html
On the AppView
object there's a function called addOne
that takes an argument:
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. 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:
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?
I'm new to Backbone and I'm confused by a snippet of the sample todo app code: http://backbonejs/docs/todos.html
On the AppView
object there's a function called addOne
that takes an argument:
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. 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:
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?
- By any chance is Todos an instance of Backbone.Collection? – Kyle Needham Commented Feb 20, 2014 at 21:19
- @KyleNeedham Yes it is. – user886596 Commented Feb 20, 2014 at 21:20
- backbonejs/#Events-catalog – Cory Danielson Commented Feb 20, 2014 at 21:43
1 Answer
Reset to default 10Backbone.Collection.add
& Backbone.Collection.create
will trigger the add
event passing model, this, options
as arguments like this.
.trigger('add', model, this, options);
this.addOne
is then passed these arguments when called by listenTo
.
本文标签: javascriptBackbone listenTo callback argumentsStack Overflow
版权声明:本文标题:javascript - Backbone listenTo callback arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236809a2363179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论