Skip to content

Instantly share code, notes, and snippets.

@trcarden
Created May 17, 2012 04:30
Show Gist options
  • Save trcarden/2716372 to your computer and use it in GitHub Desktop.
Save trcarden/2716372 to your computer and use it in GitHub Desktop.
Corrected Backbone View Advice
// Instead of
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
this.el.html( template );
}
});
// Do this instead
SearchView = Backbone.View.extend({
constructor:function () {
var that = this;
that.initialize = function(){
that.render();
};
that.render= function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
that.el.html( template );
};
return Backbone.View.apply(that, arguments);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment