Created
December 20, 2015 03:34
-
-
Save snelson/c5fddf56eb898eb30421 to your computer and use it in GitHub Desktop.
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
$(document).on 'click', 'a', (e) -> | |
$link = $(e.currentTarget) | |
href = $link.attr('href') | |
# we only care about links with http protocol | |
isHttp = $link.prop('protocol').indexOf('http') == 0 | |
# we only care about links to html pages | |
allowedFormats = ['html'] | |
format = getFormatFromUrl(href) | |
isAllowedFormat = format in allowedFormats | |
# allow this behavior to be disabled with data-pushstate=false | |
isPushState = $link.data('pushstate') != false | |
# ignore links with data-remote=true | |
isRemote = $link.data('remote') | |
if !e.isDefaltPrevented() && !isRemote && isHttp && isAllowedFormat && isPushState | |
e.preventDefault() | |
# trigger the router and make sure the route event is triggered | |
app.navigate(href, trigger: true) | |
# This helper function returns the format of a given url | |
# (what comes after the '.') | |
getFormatFromUrl = (url) -> | |
dotIndex = url.indexOf('.') | |
if dotIndex > 0 | |
return url.slice(dotIndex+1) | |
else | |
return 'html' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment