Skip to content

Instantly share code, notes, and snippets.

@sulf
Created September 28, 2011 21:49
Show Gist options
  • Save sulf/1249349 to your computer and use it in GitHub Desktop.
Save sulf/1249349 to your computer and use it in GitHub Desktop.
_.extend Backbone.Router.prototype, {
# load a new fragment, replacing the current history entry with the new fragment
# this is used to prevent a back-button-redirect loop
redirect:(fragment) ->
Backbone.history.redirect(fragment)
}
_.extend Backbone.History.prototype, {
redirect:(fragment) ->
frag = (fragment || '').replace(/^#*/, '')
if @fragment == frag || @fragment == decodeURIComponent(frag)
return
@fragment = frag
if @_hasPushState
# this is copied from the backbone source, but I'm not sure it works as expected
# (at least not with our configuration) due to the way the new location is assembled
# I believe it's missing '#' and '/' characters in appropriate positions
#
# In any case, we don't currently request pushState on initialization, but should we
# decide to switch, this must be investigated.
if 0 != frag.indexOf(@options.root)
frag = @options.root + frag
loc = window.location
new_location = loc.protocol + '//' + loc.host + frag
window.history.replaceState({}, document.title, new_location)
else # not pushState
window.location.replace('#' + frag)
@loadUrl(@fragment);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment