Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created July 10, 2012 22:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1Marc/3086752 to your computer and use it in GitHub Desktop.
Save 1Marc/3086752 to your computer and use it in GitHub Desktop.
Cleaning up finding elements in Backbone Views
// Add this to your backbone view:
selectors: {
form: '.ui-page-active form',
backbutton: '.ui-page-active .back',
...all your selectors...
},
$find: function(key) {
if (this.selectors.hasOwnProperty(key)) {
return this.$(this.selectors[key]);
}
return this.$el;
},
// Then use this.$find('form') or whatever to find the element you need.
// This way your view isn't scattered with selectors and it's easy to configure them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment