Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created September 25, 2012 16:40
Show Gist options
  • Save srstanic/f61053b68bc585676361 to your computer and use it in GitHub Desktop.
Save srstanic/f61053b68bc585676361 to your computer and use it in GitHub Desktop.
Overriding Backbone.View constructor example
/**
* Store view's clientId to the root element's $.data object.
*/
var MyView = Backbone.View.extend({
constructor: function(){
var view = this,
render;
Backbone.View.prototype.constructor.apply(view, arguments);
if (view.$el) {
view.$el.data('cid', view.cid);
} else if (view.render){
render = view.render;
view.render = function(){
render.apply(view, arguments);
if(view.$el) view.$el.data('cid', view.cid);
}
}
}
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment