Skip to content

Instantly share code, notes, and snippets.

@glyphobet
Created November 9, 2012 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glyphobet/4046010 to your computer and use it in GitHub Desktop.
Save glyphobet/4046010 to your computer and use it in GitHub Desktop.
Dispatch click events on <a>nchor tags to Backbone.js's Router
// This is the proper way to catch click events on <a>nchor tags and dispatch them to Backbone.js's Router instead
$('a').live('click', function (event) {
var href = $(event.target).attr('href');
if (href && // href attribute is defined
! /^\w+\:/i.exec(href) && // href does not begin with 'protocol:'
event.which == 1 && // first mouse button was pressed
! event.metaKey ) { // Command (Mac OS) or Ctrl (Windows) was not held down (otherwise the user wanted a new window from this link)
app.router.navigate(href, {trigger:true});
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment