0
votes

In addOne(todo) I'm having trouble understanding what the parameter is being referenced from:

In the AppView:

initialize: function(){
    Todos.bind('add', this.addOne, this);  //So "this" keyword is being passed, which refers to AppView itself
    //More code here
},

then in the addOne function,

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

See annotated source here: http://backbonejs.org/docs/todos.html

So isn't AppView being passed into addOne(todo)? Shouldn't a model be passed into model: todo in addOne()?

Thanks

1

1 Answers

0
votes

The this that's being passed as the third parameter to the bind function is used to bind the context of the function to the current context, its not being passed as a parameter. Under the hood bind uses underscore.js's _bind().

EDIT: I think what's confusing you is that it's not obvious where the addOne method is being called. Basically the add event is fired when a new item is added to the collection, you are able to bind to this event and backbone will pass the newly added model to the method that is bound to this event (so in this case the addOne method is bound to this event and the todo is the new model). What's triggering the add event in this example is when a new todo is created through the Todos.create({title: this.input.val()}); in the AppView's createOnEnter method.