routes.js
February 14th, 2007
Once upon a time there was a web framework in Java called Sails. It was pretty much a knock-off of Rails, and it was pretty much the best Java code ever written (whatever that means). All of us who worked hard on Sails (all two of us) are now programming Rails full-time and loving it. Well, mostly loving it.
We still miss parts of Sails. Parts of it were better (better!) than Rails. I’m not kidding. We still talk about Sails Components. Sails Components were amazing. We wrote like three of them. They were a late innovation on the last Sails project ever. But they were awesome.
Basically you bundled together related Java, Javascript, CSS, and template code and it all got wired up automatically. The files were included in the right spots. The ajax callbacks were defined in javascript for you. The DOM elements were already found for you, and they always had unique and meaningful ids. Data got converted to JSON and passed to the client for free, and could be sent back as context with your ajax request for a nickel. It was beautiful. But the structure it provided for heavy scripting was even better than the automation.
In Rails, now, we have this RJS thing. It’s alright, I suppose. It does encourage you to be light on scripting, which might be considered a good thing. It doesn’t embrace the web the way REST does, which is a bit concerning. And you couldn’t do anything with it if you didn’t depend on the javascript libraries, which are heavy scripting themselves—heavier for being stand-alone the way they are.
I think that if we implemented something similar to Sails Components in Rails, we would find the Sails approach to be superior for highly dynamic pages and widgets, and that RJS still has its place on simpler screens.
Anyway, I was hacking around with routes and came up with something comparable to the ajax magic in Sails. This much works:
Routes.someObjectUrl(24);
// => '/someObject/24'
Routes.someObjectUrl(24, {_method: 'delete'});
// => '/someObject/24?_method=delete'
It would probably be better to return a Url object so you can do things like this:
Routes.someObjectUrl(24).send({onSuccess: this.callback.bind(this)});

Sorry, comments are closed for this article.