Welcome!

AJAX & REA Authors: Yakov Fain, Andreas Grabner, Lori MacVittie, Kevin Hoffman, John Gannon

Related Topics: AJAX & REA

AJAX & REA: Article

AJAX Book Preview: "Controller"

A component class abstracts some behavior and to implement that, it might add a set of event listeners to its elements

This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs for the special pre-order price, click here for more information. Aimed at everyone from enterprise developers to self-taught scripters, Real-World AJAX: Secrets of the Masters is the perfect book for anyone who wants to start developing AJAX applications.

Controller
In the previous section we discussed a set of abstractions that are sub-classes of the component. A component class abstracts some behavior and to implement that, it might add a set of event listeners to its elements. Those event handlers are encapsulated in the component class and serves as its controllers. But in a bigger scheme of things, they are fully contained in the component class and can be considered a part of the View.

Besides component-specific listeners, there is a set of application-specific event listeners that respond to user inputs or timer events. These are called controllers. An event handler is a trigger point for some behavior implemented by the rest of the controller. This behavior can commonly be categorized as follows.

  1. Controllers that simply change the state of the view such as color, enabled/disabled, hide/visible, and font based on some application logic.
  2. Controllers that trigger re-rendering of the UI or its part due to some data change or navigation.
  3. Controllers that execute some business logic or do computation. Here you need to worry about the security issues based on other parameters such as if the application is running over an intranet or Internet. Based on such parameters, the business logic can be executed in the browser space or delegated to the server. In almost all cases, if some other data look-up is needed, then it's recommended that the task be delegated to a back-end service through the controller servlet.
  4. Controllers that fetch data from the back-end server, update the client model, and send a trigger to the View for re-rendering data.
  5. Timer controllers that do something periodically. For example, a timer can periodically check for the availability of new updates on the server.
Controllers encapsulate application logic-specific details. Some of these details can be abstracted as base classes in the application code. We'll visit some of these controllers in the context of an application in the following sections.

MVC Interaction
We've described the Model, View, and Controller components of the MVC pattern. Now let's consider a typical sequence of operations on the client side to understand the interactions among them better.

  • As a result of access to a URL by the user, the browser downloads a page and triggers the execution of certain controller code due to its "onload" event.
  • In the first step, the controller retrieves the HTML template from the server using an AJAX call.
  • Next the controller retrieves data from the server using an AJAX call and inserts it into the cache.
  • Then the controller instantiates one or more components, creating an association between the components and HTML elements.
  • To populate the data, the controller invokes a "setValue" call on one or more components.
  • Lastly, the controller adds application-specific event handlers to the elements.

This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs, click here to order.

More Stories By Anil Sharma

Anil Sharma is a founder of Vertex Logic and architect of its AjaxFace product. He is working with Vertex Logic's customers and helping them deploy WEB 2.0 applications built using AjaxFace. These applications are in the areas of online printing, community WEB sites and social networking. He is driving future products of Vertex Logic. Prior to Vertex Logic, he was CTO and founder of Softrock Systems and Component Plus. There he built a model driven application platform using J2EE. His primary interests include user interface infrastructures and model driven applications. He is one of the contributors of the book "Real World AJAX - Secrets of the Master".

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.