Previous topic

A Brief Introduction to Kid Templates

Next topic

Getting Started With Identity

A Brief Introduction to MochiKitΒΆ

While TurboGears is a Python-based megaframework, a modern webapp has to use JavaScript to provide a good user experience. JavaScript includes some of the conveniences that we, as Python programmers, are used to: built-in dictionaries (hashes) and listish arrays, regular expressions, functions as objects that can be passed around, etc. JavaScript also lacks a number of the tools that we’re used to. The basic JavaScript library also does not include everything you’d need for a complete app.

MochiKit fills in a lot of the gaps in a way that is both natural for Python and JavaScript. It is broken down into several packages, but you can have immediate access to all of its functionality by adding one line to your page’s <HEAD>:

<script src="${tg.tg_js}/MochiKit.js"/>

Your templates automatically get the tg.tg_js variable, and MochiKit is automatically made available as a static file from the server. When you load MochiKit this way, you’re getting everything from the MochiKit modules. MochiKit includes:

manage asynchronous tasks (including AJAX/XMLHttpRequest)
functional programming and useful comparisons
painless DOM manipulation API
“what time is it anyway?”
string formatting goes here
itertools for JavaScript; iteration made HARD, and then easy
we’re all tired of alert()

MochiKit includes so much functionality, that this module breakdown makes it much easier to follow (and work with, if you’re making improvements to it).

MochiKit’s documentation is very good, so there’s no reason to duplicate it here. To get an idea what each module has to offer, take a look at the introductory material at the top of each module’s documentation.

It is worth skimming through the function reference as well, because each module has features that will make your life easier. For example, you know from the synopsis that MochiKit.Base offers features like map and repr... but did you know that it also has functions for putting together and parsing HTTP query strings?

On the last page of the 20min wiki tutorial you find an example how MochiKit can be used to pep up a TurboGears application.

Previous: Template Variables You Get for Free