by Matt Brennan

Sodor

I’m pretty psyched about this. First, some background. As you may know, I’ve been working on an MVC framework on and off for the last four years. Mostly off. It’s called Gusto, apart from when it’s called Struct or Juego. It’s never been anywhere close to ready, and in the last four years I’ve kind of grown up as a programmer and gotten a bit disillusioned with the whole MVC thing. Also frameworks. So fuck MVC. Also frameworks. And that’s a blog post for another time.

In the spirit of modularity, Sodor is the C in MVC. It’s a standalone module to create Controllers (well, Routers, really), without any ideas about Models and Views and where they should go. It’s up to you to marshal the data and display it, but Sodor gives you a damn fine way of talking about how they fit together:

require! sodor.Controller

class Blog extends Controller
  home: @root -> render 'home.html'
  post: (id)-> render 'post.html', Posts.get id
  update: @put (id)-> Posts.set id, get-post-data @request

Firstly: any resemblance render and Post may have to actual code is purely coincidental. They don’t exist. Yet. Secondly: Function parameters like id become URL parameters, so the route ends up being /blog/post/:id. Thirdly: @root and @put might look like decorators, but they’re actually class methods. Livescript’s has the class itself as the context within its body.

If you’re wondering how it looks in Javascript, have a gander:

var Controller = require('sodor').Controller;

var Blog = Controller.extend('Blog', {
  home: Controller.root(function() { return render('home.html'); }),
  post: function(id) { return render('post.html', Posts.get('id')) },
  update: Controller.put(function(id) { return Posts.set(id, getPostData(this.request)) }
});

Pretty nice. Now, these classes don’t actually do anything on their own. Creating a class shouldn’t mutate a list of routes, or start a server, or do anything else spooky. Nah. Once you have your controller, you need to give it to Livewire:

var Blog = require('./app/controllers/blog');
var lw = require('livewire');

module.exports = lw.route(
  function() { return "404" },
  Blog.routes()
);

Serve it up with fantasy-http and you’re golden.

There’s more to come for Sodor, some pretty interesting stuff in store for Livewire (streams rewrite, browser routing), as well as the promised blog post on MVC.

Shit, I can’t believe Gusto’s actually going to happen this time.