Adding JSON with MochiKit to the `TurboGears 2 Wiki Tutorial `_ ========================================================================================================================= .. note :: This info is just copied over from the TG1 wiki tutorial, and needs to be vetted, expanded, and edited. This part of the tutorial is not technically AJAX. The "X" in AJAX stands for XML. I'm going to use `JSON`_ instead. JSON is easy and lightweight and efficient to use for all browsers. To use JSON for this TurboGears example, we just have to tell TurboGears that we want to use it via a second ``@expose()`` decorator. .. _JSON: http://www.json.org/ .. parsed-literal:: @expose("wiki20.templates.pagelist") **@expose("json")** def pagelist(self): pages = [page.pagename for page in Page.select(orderBy=Page.q.pagename)] return dict(pages=pages) Now, if point your browser at http://localhost:8080/pagelist?tg_format=json, you'll see your pagelist in JSON format. Here's an example of how the JSON output looks like:: {"tg_flash": null, "pages": ["FrontPage", "SandBox", "MyPage"]} This easy conversion to JSON is the other use for returning a dictionary. In standard CherryPy methods, you can return a string containing the rendered page. You can do that with TurboGears as well, but a free JSON interface is a pretty good reason to to it the TurboGears way. You can return as many formats as you like by stacking more ``@expose()`` decorators. Check out the `@expose reference`_ article for details. .. _@expose reference: 1.0/ExposeDecorator from MochiKit import * ---------------------- For the client side of this tutorial, we'll be using MochiKit, the official javascript framework of TurboGears. The two are only loosely coupled, as opposed to prototype/Rails, but we find MochiKit to provide a very elegant Python-inspired API while avoiding monkeypatches on the JavaScript datatypes. To keep this from turning into a javascript tutorial (it's pretty long as-is because we don't expect Pythonistas to be javascript masters), we're just going to Ajaxify one call by changing our "view complete page list" link to pull in the pagelist and include it right in the page you're viewing. The first thing we need to do is have MochiKit included in all of our pages. This can be done by editing the ``master.kid`` file or by having TurboGears add it as a widget. We'll use the latter technique here. Open the ``wiki20/config/app.cfg``. This file controls environment-independent settings like identity and output encoding. Search through the file for the ``tg.include_widgets`` setting, uncomment it, and modify it like so:: tg.include_widgets = ['turbogears.mochikit'] As the name indicates, we actually are using TurboGears' widget infrastructure to do the inclusion. Widgets are an advanced feature and are out of scope for this tutorial, but they're essentially self-contained bundles of HTML+behavior code that make building forms a snap. One of the things they can do is include javascript in a page and the ``turbogears.mochikit`` takes advantage of this to provide a javascript library. The `cogbin`_ has other javascript libraries packed up as widgets that can be used the same way. .. _cogbin: http://www.turbogears.org/cogbin/ After making this configuration change, **restart the server.** The auto-reload functionality only detects changes to python files. Prep the page ------------- Now that we have MochiKit, we're ready to modify our template. We'll practice good style by progressively enhancing our pagelist link in ``master.kid``: .. parsed-literal::