10 notes &
The el property in Backbone.js and events
Today I found myself struggling with Backbone.js events. In this particular case I wanted a span element with the class use to point to a router url. That shouldn’t be that hard to do, but I couldn’t get it to work.
After a while I realized that I overwrote the el property with the template I used instead of adding it to the el property.
render : function() {
this.el = this.template( this.model.toJSON() );
return this;
},
When I added the the template to the el property instead of making it the element, all of the sudden it worked.
render : function() {
$( this.el ).html( this.template( this.model.toJSON() ) );
return this;
},
Lesson learned: do not overwrite the el property in your render method!