The Javascript MVC framework: Part 3
It’s possible to make the Javascript MVC framework we’ve built even more like Django.
To the file that contains the createHashHandler function, add a url function. It corresponds to Django’s django.conf.urls.defaults.url function, and it’s very simple:
var url = function (regex, controller) {
return {
re: regex,
controller: controller
};
};
Now your initialization function can look like this:
/*global $, createHashHandler, url, handleDigits, handleWhitespace, handleWordChars, handleAllCaps */
$(function () {
var urlPatterns = [
url(/(\d+)/, handleDigits),
url(/(\s+)/, handleWhitespace),
url(/(\w+)/, handleWordChars),
url(/([A-Z]+)/, handleAllCaps)
],
handleHashChange = createHashHandler(urlPatterns);
$(window).bind('hashchange', handleHashChange);
handleHashChange();
});
If you’ve used Django, you can see that this now looks exactly like a Django urlconf file.