I am learning Backbone and I am trying to figure out what library I am getting the ‘on’ function out of. I thought it was jQuery, but if so I am not understanding the API. Could someone please explain the ‘on’ function or link me to some docs. The first parameter is the event. The second parameter is the function being called. What does the last ‘this’ refer to (I assume the calling class) and why is it needed? Here is my code straight from Addy Osmani, this is the AppView:
initialize : function() {
this.input = this.$('#new-todo');
this.allCheckbox = this.$('#toggle-all')[0];
this.$footer = this.$('#footer');
this.$main = this.$('#main');
window.app.Todos.on('add', this.addOne, this);
window.app.Todos.on('reset', this.addAll, this);
window.app.Todos.on('change:completed', this.filterOne, this);
window.app.Todos.on("filter", this.filterAll, this);
window.app.Todos.on('all', this.render, this);
app.Todos.fetch();
},
The on method in this case is coming from Event module of Backbone. It accepts three parameters – the event name, function and the context.The context decides what the value of ‘this’ should be inside the function.
Todos.on("filter", this.filterAll, this);your just asking that inside the function filterAll the value of ‘this’ should be your views instance