README.md

What?

SugarSkull is a client side URL router. It's the smallest amount of glue needed for building dynamic single page applications.

Why?

Storing the some state of an application in the URL allows the URL of the application to be passed between users and bookmarked, it also allows the back and forward buttons to keep track of changes to the UI.

The HTML5 history API is NOT a replacement for using location.hash. It does not cater to a single-page apps. In fact, it is designed around the requirement that all pages should load without the ability to leverage Javascript. This is unfortunate for script-rich applications who's audience is well known. The HTML5 history API requires the URL to resolve to real assets on the server, and therefore can not be used as a superfluous state management mechanism. That's where SugarSkull comes in.

Why not backbone.js? Backbone.js has limited support for this and covers a minute set of the cases for client side routing. What about sammy.js? Sammy.js is a jquery plugin that tries to emulate server side routing which introduces unnecessary concepts.

Are single page apps a problem for SEO? Yes and No. SugarSkull is meant for script-heavy web-apps, you can use it for script-heavy web-sites, but learn how google and other search engines crawl and index pages before you decide on anything.

How?

SugarSkull monitors the URL. When the URL changes, and it is a match to one defined in your router table, the functions that are associated with that route are executed. You could almost think of the URL as an event emitter.

More specifically the way this works is that we divide the url into two parts. First the server-side (everything
before the '#'), and then the client-side (everything after the '#'). The second part is the HashRoute.
A hash route looks like this...

Usage

First, the router constructor accepts an object literal that will serve as the routing table. Optionally, it can also accept a second object literal that contains functions. The second option is useful when the functions to be called get defined or loaded after the router gets defined.

A trivial demonstration

var router = new SS.router({
'/dog': bark,
'/cat': meow
});

In the above code, the object literal contains a set of key/value pairs. The keys represent each potential part of the URL. The values contain instructions about what to do when there is an actual match. bark and meow are two functions that you have defined in your code.

In the above code, you'll notice that you can also use regular expressions inside the URLs. The capture groups from the regular expressions are then sent to the functions as parameters, one after the other (a, b, c, etc).

More special events

It is common to need a particular function to fire every time a route is matched, no mater what route it is. In this case beforeall can be defined at the top level of the router definition. Similarly, leaveall will be fired when leaving all routs, and notfound will be fired when none of the routes can be matched against the user's request.

It is possible to attach state to any segment of the router, so in our case above if /dog is reached, the current state will be set to { needy: true, fetch: 'possibly' }. Each nested section will merge into and overwrite the current state. So in the case where the router matches /cat/hungry, the state will become { needy: true, fetch: 'unlikely', frantic: true }.

leaveall - A function or array of functions to execute when leaving any route.

Credits

Version

Licence

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.