Wednesday, October 22nd, 2008

Reinhardt: Client-side URL Dispatching from SitePen

Category: Dojo, JavaScript

Kevin Dangoor from SitePen recently announced the release of a small pet project: Reinhardt. From the blog:

A typical server-side web framework today includes three main components: a URL dispatching to some controller object scheme, a template engine, and a data mapping facility. Currently in Dojo, you’ll find that the latter two items already exist. dojox.dtl provides the first one, and dojo.data provides the second.

Given that Dojo already offers an implementation of the Django template engine, and that regular expression-based URL dispatch was a good fit for our problem at hand, I decided to [create a] URL dispatch on the model provided by Django.

Reinhardt is the name I gave to this client-side web framework.

Using Reinhardt, you pair regular expressions to method calls:

javascript

  1. var patterns = reinhardt.urldispatch.patterns("demo.code",
  2.   [/^list\/(\d*)$/, "showMovieList", ["year"]],
  3.   [/^search$/, "search"],
  4. );

(The first argument to the patterns function provides the URL prefix required for Reinhardt to dispatch a URL.)

Hacking Support for Groups

Because JavaScript regular expressions don’t support named groups, you can pass in a group name as a third parameter:

javascript

  1. [/^movie\/(\d+)\/$/, "whatyear", ['id']],
  2. [/^movie\/([\w ]+)\/$/, "whatyear", ["name"]],

Kevin explains further about this feature:

These two entries show that the patterns are matched in order. movie/10/ would match both of the expressions. But, movie/Cars/ will only match the second. If the first one is a match, demo.code.whatyear is called with an ‘id’ attribute on the parameters object. If the second matches, there will be a ‘name’ attribute passed to that same function.

Kevin imagines a future where this hack could go away:

JavaScript regular expressions do not support named groups as Python’s do. Adding named groups would likely be a fairly simple extension to the syntax, but is not something that I’ve done. At some point in the future, there may be the option to pass in a string that can include named groups rather than just a standard regex object. Similarly, since Reinhardt does not do any parsing of regular expressions, it has no way of putting values back into a URL pattern to generate a URL.

Not Quite a Project Yet

At this point, it simply offers URL dispatch. I can imagine a client-side framework offering tools to help you organize large Dojo-based applications in easy to understand, simple to grow manner. The built-in URL dispatch could also provide automatic history and back button support. For now, however, it is just a small bit of code to make client-side URL dispatch clean and simple.

I should note at this point that Reinhardt is more of a demo than a real open source project. However, it is a straightforward implementation and includes unit tests, so you can confidently use it for real URL dispatch work.

Posted by Ben Galbraith at 7:00 am
Comment here

+++--
3.6 rating from 21 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.