Last active
August 29, 2015 14:05
-
-
Save jshaw/6a9609a2ec3fa99e280a to your computer and use it in GitHub Desktop.
example using underscore binding on events to keep class scope
This file contains hidden or 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
| // When using underscore this passes the class scope into the event handler (callback function) | |
| // rather than having the click event scope in the handler (callback function) | |
| Navigation.prototype._addClickEvent = function () { | |
| var handler = _(this._handleEventClick).bind(this); | |
| this.$nav_items.on('click', handler); | |
| }; | |
| // Here this refers to Navigation.prototype rather than the event which gets passed in | |
| Navigation.prototype._handleEventClick = function (evt) { | |
| evt.preventDefault(); | |
| this.$currentTarget = $(evt.currentTarget); | |
| this.selected_nav = this.$currentTarget.data('section'); | |
| this.selected_nav_slug = this.$currentTarget.data('slug'); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment