Created
July 10, 2012 22:49
-
-
Save 1Marc/3086737 to your computer and use it in GitHub Desktop.
Cleaning up finding elements in Backbone Views
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
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. |
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
*deleted* |
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
// 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