Event listeners in Sencha Touch 2, the Ext JS approach

I have been developing Ext JS for a long time now and recently I have started with Sencha Touch 2. And while I am at it, I have the feeling that the Ext JS team and the Touch team are lately not in the same building.

When you only have experience with Sencha Touch maybe this article is not for you. When you only have experience with Sencha Touch you don’t know any different. But personally I like to see the good parts from Ext JS in Sencha Touch.

I know that you can build classes like in Ext JS. But since I also use Sencha Architect, I like to keep my classes syntax close to the Architect. I am using Architect mostly for prototyping and not as program generator.

Event listening the Ext JS way

In Ext JS a listener to a button looks like this:

The trick is in the scope. The scope is on this, the main object where this button is part of, like a panel or a toolbar. So calling this.onNiceButtonPressed, is simply a function within this object.

Leaving the scope out, it would have the scope on the button. It works the same with listeners.

The listeners are calling functions within the object (this) which also can be called by other objects (public). This is helpful if you are using controllers that also have to call these functions. Such a function could be reloading a store, what could happen by pressing a button or as an additional action with a change of a another panel listened by the controller.

Event listening Sencha Touch 2 way

What I like about the Ext JS approach is that I have my listeners always close to the actual component. But Sencha Touch is different. It works with listeners on the main object that delegate to the component where you want to add a listener to. This sounds confusing but it looks like this:

What you see above is that the listeners are all put below the config in listeners array. In this sample I want to create the simple result of firing events on the main object (in Ext JS the this object), that I listen for in a controller. I don’t like it when my listeners are "drifted" away from the actual component.

Bad according to Sencha

Sencha says that it is bad to put your listeners at config time. Nevertheless I am doing it, to get closer to what I am used to with Ext JS. The result is not so gently as Ext JS, but close enough.

Item configuration

What I am doing is simply use the up method on the button to get to the object where I want to fire my event on. I am using the widget name of the object that I want. The up function works fine, because the component is always subordinate to the main object I choose. You don’t have to use the itemId on the panel for this.

Controller

The controller has the following event listeners:

Adding events at initialization

It is also possible to add new events at initialization of the object. For this you add the initialize() function to the definition of your class.

Who is firing and who is listening

I don’t like it when I have to browse through a lot of different sources to find related functions. I like to share a couple of rules that apply when I design applications.

Controllers manage more objects

Controllers manages more objects – only a controller may trigger functions in other objects. So when action in object A leads to a change in object B, it will be handled by a controller. Object B doesn’t listen to events of object A. The controller handles this.

Objects can take care of themselves

All functions unique for an object are handled by that object. When in a dataview a searchfield is changed, the dataview object will handle this event and updates the store. There is no request going to a controller. I use "exit" events that are fired when an action is completed. For example when an Ajax call is successfully completed I could fire and event like "ArticleStoreLoaded".

Supply fire events with enough parameters

When firing an event, add the right parameters. It is annoying when the event is listened for and the function executed has to look up all the information that was available at firing time.

Controllers listen to events

I never put listeners in controllers to components in other objects. A controller listens only to events that have been fired by other objects.

Separate classes

Separate all panels lists and dataviews in different view classes to keep the sources small. That doesn’t count for toolbars.

Fire events on the main object

Fire all events on the parent (Ext JS this object) to limit my references in the controller.

Stick to the framework

Stick to the rules of the original framework. Exceptions are most of the time the reason of bugs when an upgrade is installed.

Good software is all about reliability, stability and performance, less about the syntax

Johan van de Merwe
Dedicated to professional software development since 1985. Has worked since 1992 as IT manager in several international operating companies. Since 2007 CEO and Sencha Ext JS web application developer at Enovision GmbH.

Comments

  1. ANILINA

    How to mix Tap and drag events both.
    Drag event should happen after TapHold.

    i am following this code “http://druckit.wordpress.com/2013/04/08/tap-and-drag-animation-domination-part-3-of-3-implementing-drag-and-drop-in-sencha-touch-2/”

    suggest me some code help.

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.