Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 224
user886596 Avatar asked Feb 20 '14 21:02

user886596


1 Answers

Backbone.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.

like image 88
Kyle Needham Avatar answered Oct 13 '22 05:10

Kyle Needham