Client-side Sammy.js + Knockout.js in Coffeescript

I’ve been working on an HTML5 client application that leverages a bunch of cool Javascript libraries to implement a sophisticated data editing and visualization editor in the browser. In slightly more detail I’m using jQuery (of course), Sammy.js, Knockout.js, and D3js together to build this little monster. And I’m doing it all in Coffeescript.

More about that later however. Today we’re going to take a quick look at how to use Sammy.js (for client side routing) with Knockout.js for data binding and view rendering. There are actually great examples of how to do this already (see: but they’re written in native Javascript, not Coffescript.

Having already written two single-page applications (a basic Sammy.js page and a not so basic Knockout.js page) in Coffeescript and after a quick peek at the Javascript it looked easy to mash the Coffeescript from these two examples together and get it working. Ten minutes later it works 90% but the 10% that’s busted is completely fracked.

To digress for a moment and talk about Coffeescript I have to say that I hate it. Actually I don’t. I’m using it a lot and it saves me a ton of time. But as a C++ programmer the idea that I can write some code, compile it cleanly and then see such an enormous variety of spectacularly evil and unexpected runtime problems is disconcerting.

That said, pilot error is pilot error. It’s just that I prefer my foibles to remain a private affair between me and the compiler. But here’s the thing: Javascript generated by wrong Coffeescript misbehaves so spectacularly that it’s usually obvious that you botched the magic sequence of glyphs that is Coffescript. And the JS debugging tools are good (although what a mess the DOM is).

So anyway, I ran into one of these situations yesterday with my 90% working first attempt to get Sammy and Knockout talking. So I went looking for inspiration (i.e. code I could copy) but couldn’t find a good Coffeescript example so I wrote one.

Code for this example is available here:

Also, here’s the example hosted on my server:

Here’s the code:



<!DOCTYPE html>

Minimal HTML5 application demonstrating the use of Sammy.js
with Knockout.js in Coffescript.

Copyright 2013 Chris Russell (
Published under the Boost Software Licene v1.0

<meta charset=”UTF-8″>
<!– lib scripts –>
<script src=”./js/libs/jquery-1.8.3.js” type=”text/javascript”></script>
<script src=”./js/libs/sammy.js” type=”text/javascript”></script>
<script src=”./js/libs/knockout-2.2.1.debug.js” type=”text/javascript”></script>
<!– app scripts –>
<!– <script src=”./js/minkosam-handcrafted.js” type=”text/javascript”></script> –>
<script src=”./js/minkosam.js” type=”text/javascript”></script>
<body style=”background-color: white;”>

Minimal HTML5 application demonstrating the use of <a href=””>Sammy.js</a&gt;
with <a href=””>Knockout.js</a&gt; in <a href=””>Coffescript</a&gt;.
<div style=”background-color: #F0F0F0; border: 1px solid #CCCCCC; padding: 10px;”>
This is a DIV that contains a DIV whose inner text is bound to the Knockout view model observable samPath.
<div id=”samPathDisplay” data-bind=”text: samPath” style=”background-color: #DDEEFF; border: 1px solid #CCCCCC;”>
STATE #1: initial value set statically in the HTML document source

[ <a href=”./minkosam.html”>./minkosam.html</a> ]
[ <a href=”#”>#</a> ]
[ <a href=”#/”>#/</a> ]
[ <a href=”#/hello”>#/hello</a> ]

<p style=”text-align: right”>Copyright 2013 <a href=””>ChrisRus</a><br><a href=””>Boost Software License v1.0</a></p>


Hand-Crafted Javascript

Extracting the essence of the solution from the e-mail client example on the Knockout.js site, I authored the following JS by hand:
This works as expected in the minkosam.html page (uncomment out the include of minkosam-handcrafted.js).

function KoViewModel() {

var self = this;
self.samPath = ko.observable("STATE #2: initial value set during the initialization of ko.observable");

var sammyRouter = $.sammy(
function() {

function(context_) {
function(context_) {

function(context_) {

function(context_) {



jQuery( function() {

var koViewModel = new KoViewModel();
//koViewModel.samPath("STATE #3: initial value set immediately following the binding of the view model");

} );


Next I went over to the Coffeescript site and sat in their interactive compiler and hacked out the following Coffeescript that produces Javascript nearly identical to my handwritten code above:

# Take a look at ../minkosam.js which was hand authored and tested.
# I then sat in the Coffeescript editor and worked out the following
# which is transformed to approximately equivalent Javascript.

class ViewModel
constructor: ->
self = @
self.samPath = ko.observable "test"

sammyRouter = $.sammy( ->
@get '', (context_) ->
@get '#', (context_) ->
@get '#/', (context_) ->
@get '#/hell', (context_) ->
jQuery ->
koViewModel = new ViewModel();


So that’s it. The combination of these two libraries is very useful.

About Chris Russell
This entry was posted in Internet, Software and tagged , , , . Bookmark the permalink.

2 Responses to Client-side Sammy.js + Knockout.js in Coffeescript

  1. il says:

    Thank you! this was helpful!

  2. This post has received quite a few hits. A quick update: Knockout.js is really cool and useful. Sammy.js is an interesting library but ultimately does more than what I want in ways that seem overly complicated to me. I’ve dropped the dependency on Sammy.js and moved on to use director.js. Director.js isn’t really exactly what I want either but it is comparably simple. See later posts on this blog if you’re interested in client side routing or Knockout.js.

Comment on this article

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s