Section 3.6. MVC Meets Ajax

3.6. MVC Meets Ajax

Recently, there has been a dramatic shift in web development paradigms. Ajax seems to be taking the world by storm, so now it is no longer a given that a user action is going to create an HTTP Post (or HTTP Get) that will then return a brand new page. Instead, user actions can be picked up by JavaScript on the page and turned into requests that go to the server and get a value back, which is then passed back to the in-page JavaScript by the browser.

Even with more JavaScript view logic, and dynamic updates on the same page, the MVC paradigm still works; you still use views to generate user actions, which are handled by the controller and returned to the browser. You don't have to generate a new page every time something changes. This can be a huge win in terms of improved user experience, and decreased bandwidth. Not only that, Ajax also enables another common MVC idiom that previously had been impossible to implement on the web. As mentioned earlier, in the olden days of GUI MVC, views were often designed to poll the model so that they could automatically update themselves whenever something changed. Without Ajax, there was no good way to do this effectively through the web, but now you can have tickers that show alerts when stock prices drop below a certain threshold, or that let you know when your friend has logged in to the same site you are using, and so on.

Unfortunately, writing Ajax-enabled applications isn't as easy as it should be. Different browsers have different request objects that communicate back to your server, and depending on which browser your user has, you'll need to pick the "right" kind of request object and code to it. Perhaps even worse is that even though JavaScript, and the Document Object Model, has been standardized for years, XML manipulation in JavaScript isn't the same for all browsers. The list of cross-browser differences is large enough that without help you could easily try it and determine that all this talk about new user experiences isn't worth the hassle.

But don't let all that get you down. Writing cross-browser Ajax applications doesn't have to be a brain bending experience. TurboGears comes with a built-in answer to many of the problems that make the JavaScript development experience painful. TurboGears includes a fantastic library of helper functions called MochiKit. MochiKit is great because it has tests that run against all the major browser platforms; so when it says that it handles browser incompatibilities for you, you can actually trust that it works!

With MochiKit, you can use the MochiKit request object to talk to the server, and be confident that your request will work in IE6, Firefox, and Safari. You don't have to use MochiKit or Ajax to build TurboGears applications, if your application just returns static pagesyou can skip past all of this new-fangled Ajax stuff and get right down to business. But, if you have a basic understanding of JavaScript syntax, and a desire to build highly interactive web applications, TurboGears includes MochiKit, SimpleJSON, and Widgets, which all work together with the standard TurboGears components to make building ultra-dynamic Ajax-enabled web applications a lot easier.

Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: