Created
November 9, 2012 14:29
-
-
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 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
// 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