Sammy.js

nyc.js / june 3, 2009

What is Sammy?

Sammy

Classy.

Sinatra

Kind of like this guy . . .

Sinatra

Classier.

The short description

Sammy is a tiny JavaScript framework,
built on top of jQuery,
inspired by Ruby's Sinatra

Zuh?

Sinatra:

# app.rb
class MyApp < Sinatra::Default

  get '/user/:name' do
    @user = User.find_by_name(params['name'])
    erb :user
  end

  post '/user/' do
    @user = User.create(params['user'])
    redirect "/user/#{user.name}"
  end

end

Sammy

// app.js
var app = $.sammy(function() { with(this) {

  get('#/user/:name', function() { with(this) {
    User.get(params['name'], function() {
      partial('user.html.erb', {user: user});
    });
  }});

  post('#/user/', function() { with(this) {
    User.create(params['user'], function(user) {
      redirect('#/user/' + user.name);
    });
  }});

}});

Why?

1. Structure without the weight

Like <10K for real.

2. Single Page Apps

I know you be sweatin' gmail.

3. Fully JavaScript applications with CouchDB/App

Turtles all the way down (with a crunchy erlang center)

4. Because it was fun

I like Javascript

Really . . .

Sammy is RESTful evented Javascript.

Basics

Routes

// routes
get('#/', function() { //... do something ... });

Routes are events triggered on URL change.

Events

// events
bind('custom-event', function() { //... do something ... });

Events are per-app, and only bound to the DOM after 'run()'

Context

get('#/', function() {
  // this == EventContext
});

bind('custom', function() {
  // this == EventContext
});

EventContext does all your renders and redirects.

MORE!

// helpers
helpers({
  autolink: function(text) {
    return text.gsub(/(http\:\/\/[^s]+)/, '<a href="$1">$1</a>');
  }
});

// befores
before(function() {
  if (!loaded) {
    // load
    return false; // stops the execution of routes.
  }
});

DEMO

Please don't fuck this up . . .

v0.1.4

seriously, it's early days

The Future!

1. More simple abstractions/features.

2. Getting rid of jQuery

3. RESTful Database Abstractions

4.

5. SUCCESS!

THANKS!

http://code.quirkey.com/sammy

Aaron Quint

Quirkey NYC, LLC

aaron@quirkey.com twitter.com/aq