My question most likely requires a very simple answer, which, nevertheless, I was not able to easily find.
A Backbone application I am working on has several views. When defining the different views, I use _.bindAll in the initialize function to connect the "this" view object with the view's render function. For example:
DiscussedItemView = Backbone.View.extend({
...
initialize: function() {
_.bindAll(this, "render");
},
render: function() {
this.$el.attr('id', 'li-meeting-task-' + this.model.getId());
this.$el.html(JST['tasks_sandbox/agenda_task_item']({
taskName : this.model.getName(),
taskId : this.model.getId()
}));
return this;
},
...
});
To create a new instance of the DiscussedItemView, I do the following:
...
var discussion_agenda_row = new DiscussedItemView({model: task});
discussion_agenda_row.render();
this.$( '#meeting-items-list' ).append(discussion_agenda_row.$el);
...
The code works fine. Still, I do not understand why there is a need for the explicit use of the render() function on discussion_agenda_row. I thought that the initialization of a new DiscussedItemView instance would automatically call the render function, but if I remove the discussion_agenda_row.render();
row, the HTML will not be displayed. Where am I mistaken?
Thank you, Alexandra