Created
September 25, 2012 16:40
-
-
Save srstanic/f61053b68bc585676361 to your computer and use it in GitHub Desktop.
Overriding Backbone.View constructor example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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