Sammy 0.4: Here’s looking at you

It’s been a good while in the making, and I’m proud to announce the next major release of Sammy.js. Also, I’ve published Part II of the Sammy Tutorial. Part II introduces post routes as well as some of the new features of Sammy 0.4.

I’ll swing through some of the biggest changes and new hotness – see the HISTORY or the Commits for a full list and more detail.


I’ve always thought the poller implementation that monitors the hash/location for changes was a little bit of a hack. CodeOfficer also had brought up the pain of having a poller per-app when you have multiple or many Sammy applications on a single page. As more browser’s support it I also wanted to make use of the native ‘onhashchange’ event which when bound to, completely eliminates the need for polling. I attempted to do this all in one fell swoop, by decoupling the location monitoring into an entirely separate object: Sammy.HashLocationProxy. The HashLocationProxy is setup for each app by default. It tries to make use of the ‘onhashchange’ event where available, and where it isn’t it gracefully falls back to ol’ fashioned polling. The big difference, however, is there is only a single global poller per page no matter how many apps you have.

The HashLocationProxy also defines a spec for other location proxies to be created. There’s also a DataLocationProxy which derives its location from a attribute.

Storage and Session

The newest and most visibly awesome part of the new release is a new suite of plugins and prototypes: Sammy.Store, Sammy.Storage and Sammy.Session. As part of 0.3 I introduced Sammy.Cache which was really a first attempt at what Sammy.Storage does. Sammy.Store is an single access point and API for all the various browser based storage methods, including HTML5 DOM Storage and Cookies. It provides a unified way of dealing with these as Key/Value stores. As an extra bonus, it also conforms to Key Value Observing, triggering jQuery events when values change. Heres a little example:

// Cookies
var cookie = new Sammy.Store({name: 'mycookie', type: 'cookie'});
cookie.set('foo', 'bar'); //=> "bar"
cookie.get('foo'); //=> "bar"
cookie.keys(); //=> ["foo"]
// HTML5 localStorage (only works in certain browsers)
var local = new Sammy.Store({name: 'mylocal', type: 'local'});
local.fetch('foo', function() {
return 'bar';
}); //=> "bar"
// Only sets if not set.
local.fetch('foo', function() {
return 'baz!';
}); //=> "bar"

The latest chapter of the Sammy tutorial also covers Sammy.Session

More Plugins

There are also a number of other less glamorous but new and awesome plugins.

  • Sammy.Mustache Provides support for the Mustache templating framework, thanks to Mustache.js
  • Sammy.JSON is a simple wrapper around json2.js
  • Sammy.NestedParams: Adds full [tested!] support for Rails/Rack style nested params to Sammy form handling. (Even though this was part of the 0.3.1 release, I never mentioned it here)


Thanks to everyone who helped with this release through testing, ideas, or putting Sammy in production and telling me what’s wrong with it.

One Response to “Sammy 0.4: Here’s looking at you”

Seven Says: #

A link like #/somelink can only click one time, then it would be non-clickable before you click another link. Is it possible to click continuously?


QuirkeyBlog is Aaron Quint's perspective on the ongoing adventure of Code, Life, Work and the Web.




QuirkeyBlog is proudly powered by WordPress