Skip to content

Instantly share code, notes, and snippets.

@scottbaggett
Created April 14, 2013 20:23
Show Gist options
  • Select an option

  • Save scottbaggett/5384065 to your computer and use it in GitHub Desktop.

Select an option

Save scottbaggett/5384065 to your computer and use it in GitHub Desktop.
death of the hashbang
# First, we should prevent the Hashbang from ever occuring by
# overriding Backbone’s ‘loadUrl’ method (Hat tip to Michael Franzkowiak):
Backbone.History.prototype.loadUrl = (fragmentOverride) ->
fragment = this.fragment = this.getFragment(fragmentOverride,
this._wantsPushState &&
!this._wantsHashChange)
matched = _.any this.handlers, (handler) ->
if handler.route.test(fragment)
handler.callback(fragment)
return true
matched
# Then we need to find a way to make
# certain links pushstate capable.
$(document).delegate "a.pushState", "click", (evt) ->
# Get the anchor href and protocol
href = $(this).attr("href")
protocol = this.protocol + "//"
# Ensure the protocol is not part of URL, meaning its relative.
# Stop the event bubbling to ensure the link will not cause a page refresh.
if href.substring(0, protocol.length) != protocol
# Leading /'s break Backbone router nav
if href.charAt(0) == '/'
href = href.substr(1, href.length)
if Backbone.history._hasPushState
evt.preventDefault()
app.router.navigate href, trigger: true
else
#Do Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment