Skip to content

Instantly share code, notes, and snippets.

@jshaw
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save jshaw/6a9609a2ec3fa99e280a to your computer and use it in GitHub Desktop.

Select an option

Save jshaw/6a9609a2ec3fa99e280a to your computer and use it in GitHub Desktop.
example using underscore binding on events to keep class scope
// 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