I’m asking this based on a blog I read…
backbone.js simple inheritance…
Where does the parameter model come from in render: function(model){...}? I know that this.render is called for every new item within the collection, but where does function(model) come from? And how can that be passed as model for SingleAnimalView like so: new SingleAnimalView({model: model})?
var AnimalView = Backbone.View.extend({
el: "#demo",
initialize: function(){
window.animals.bind("add", this.render, this);
},
render: function(model){
var singleAnimalView = new SingleAnimalView({model: model});
$(this.el).append(singleAnimalView.el);
}
});
Here’s the jsFiddle link: http://jsfiddle.net/HVK7F/
When the
addevent is fired from theanimalscollection, the callback method’s first argument will be item that was added to the collection.In this case,
render(model)is being used as the event handler, and model will be the item added.Have a look at the
addmethod of the annotated source to see how it’s invoked. Ultimately it’s a result of this line:trigger()takes all of the arguments except the first one and passes them to the callback.