GXT’s MVC Framework

image

The AppEvent class

The messages that pass between controllers and views are instances of the AppEvent class. Each AppEvent object has a specific type defined by an EventType object.

Optionally, an AppEvent can contain a payload of one or more items of data by using the setData methods. This is useful for passing the state information. If we want to include more than one data object in an AppEvent, we need to pass a key as a String to allow us to retrieve that object later.

The EventType class

An EventType defines a custom type of Event that can be used to set the type of an AppEvent.

Typically, we will define each EventType a static field in an AppEvents class. We will now define two EventType objects for the example application.

Controller class

A Controller processes and responds to events in the application.

A Controller must register the event types it wishes to observe in its constructor. The registerEventTypes method is used for this and takes EventType objects as parameters.

The View class

The View class is the part of the GXT MVC framework that provides the user interface. It is responsible for displaying components and reacting to events forwarded from the Controller. It also responds to user actions by forwarding the AppEvents to the Dispatcher.

Like controllers, views are required to implement the handleEvent method. To keep the code tidy, it is helpful to create an on method for each EventType.

Dispatcher

Dispatcher is a singleton class, a class that is limited to a single instance that is available across the application. It has static methods that can be used to forward AppEvent objects. A Controller registers with the Dispatcher to observe AppEvent objects of a specific EventType. When an AppEvent is dispatched all Controller objects that have registered to observe the EventType will be notified with the AppEvent.

An event can be fired from anywhere in the application using one of the static forwardEvent methods of the Dispatcher. There are four convenient methods to the forwardEvent method that take different arguments.

Incorporating MVC

As we are now using the GXT MVC framework, this gives us an opportunity to re-factor the code to make the individual components more self-contained.

In order for a Controller to start receiving events, it needs to be registered with the Dispatcher. This is normally done in the EntryPoint class and that is what we are now going to do in our example application.

 

Here is an code  example

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s