I think mateusmaso's answer is mostly right, but it needs some tweaking to ensure that you always get the right url you are looking for.
First you need to override the route method to run the beforeRoute method:
route: (route, name, callback) => Backbone.Router.prototype.route.call(this, route, name, => @trigger('beforeRoute') callback.apply(this, arguments) )
Then you bind the event and initialize the history instance variable:
initialize: (options) -> @history = [] @on "beforeRoute", @storeRoute
Next, create helper methods for storing and retrieving the fragment:
storeRoute: => @history.push Backbone.history.fragment previousFragment: => @history[@history.length-2]
Finally, you need one last helper method that you can use to change the URL without reloading and saving the resulting snippet. You must use this when closing the popup or you won’t have the expected fragment in your history if the user pops up again without moving to another location. This is because a navigation call without "trigger: true" will not call an event handler to store the fragment.
changeAndStoreFragment: (fragment) => @navigate(fragment) @storeRoute()
Drewb
source share