documentcloud/backbone — pull request #2583 by petehunt (2013-06-03 06:13:50+00:00)
Title: Make Backbone.Router work on the server

This makes Backbone.Router work on the server. Well, it actually just refactors it such that it would be quite easy to make it work on the server.

I'm trying to do the same thing https://github.com/documentcloud/backbone/pull/1541 did.

Backbone.History is the dependency on the browser that needs to be fixed. I did this by pulling out all the code that manages routes (parsing, normalization, keeping track of calbacks, etc) into a RouteMapper object. Backbone.History now delegates all of that responsibility to RouteMapper.

It seems like there's a lot of assumptions about how URLs should be normalized based on whether they came from the hash or the push state. These assumptions (calls to getFragment()) are threaded throughout the code. Rather than unraveling this I introduced the idea of a raw fragment, which is just the raw string from the browser and a boolean indicating whether it's a path or a hash. This seemed to be the easiest (and a reasonable) way to keep the decoupling between the browser environment and path management intact.

As this is my first pull on Backbone I'm sure I got some stuff wrong and did some non-idiomatic things, but from an architectural perspective I think this is pretty good. I included a test case which indicates what a server-side History would look like.

lint had some warnings but not in my code; I left them alone but if you want me to fix them I'd be happy to.