Chapter 6. Controllers

In this chapter we are going to talk about controllers. They are the last piece of the MVC architecture provided by Sencha Touch, and they provide many services that are very useful for software developers creating mobile applications.

Director of the Orchestra

To put it bluntly, the primary objective of controllers is to concentrate power. They provide an interaction layer that sits somewhere between models, stores, and views, and they act as the director of an orchestra, reacting to events, making decisions, and changing the state of the application as needed.

As you saw in Chapter 3, Sencha Touch allows you to provide event handlers for your views, including code that will be executed whenever the user triggers an event on a particular control on the application. These event handlers can be included in the configuration section of any control, using the listeners key.

However handy, this approach leads to unmaintainable and non-reusable code in the long run. It makes the code difficult to learn and debug, not only for new team members, but also for the original team who wrote the code. Finally, it makes reusing code more difficult; what happens if you have several controls whose event handlers are similar, or strictly the same? Should you cut and paste the code?

Controllers provide a concrete answer to this question, including many other interesting features at the same time:

Routing and deep-linking
Using controllers, you can provide URLs to access different ...

Get Sencha Touch 2 Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.