Skip to content

Instantly share code, notes, and snippets.

@snelson
Created December 20, 2015 03:34
Show Gist options
  • Save snelson/c5fddf56eb898eb30421 to your computer and use it in GitHub Desktop.
Save snelson/c5fddf56eb898eb30421 to your computer and use it in GitHub Desktop.
$(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