Wednesday, 24 April 2013

How to fire Custom Events with Parameters in Sencha Touch 2

In Sencha Touch 2, Every view components supports the various list of events like initialize,activiate, painted,  tap, show, hide and so on. In  addition to that, Sencha Touch also supports for adding custom events by using fireEvent() function. This function accepts the following parameters
  • event Name: The name of the event to fire
  • args: variable number of parameters are paased to the event handler
This function will returns boolean (true/false).

Let me demonstrate this feature with example, which display the Toolbar with login button. By clicking on the login button, we will fire the custom event for the toolbar view component . We are also going to see, how the fired custom event will be mapped with the handler function in the controller.

Create a Toolbar with Button

First, we will create a toolbar with login button. Using listeners configuration, we are attaching the tap event to the function onLoginButtonTap() that needs to be called when the tap event fires. Inside the function, we will fire the custom event for the toolbar view component with event name as  'loginButtonTap' and passing parameters to the handler function.
Ext.define('MyApp.view.MyToolbar', {
    extend: 'Ext.Toolbar',
    alias: 'widget.mytoolbar',

    config: {
        id: 'mytoolbar',
        itemId: 'mytoolbar',
        docked: 'top',
        itemId: 'toolbarSetting',
        items: [
            {
                xtype: 'button',
                itemId: 'loginButton',
                id: 'loginButton',
                text: 'Login'
            }
        ],
        listeners: [
            {
                fn: 'onLoginButtonTap',
                event: 'tap',
                delegate: '#loginButton'
            }
        ]
    },
    onLoginButtonTap: function(button, e, options) {
        var me = this;
        var toolbar = this.up('#mytoolbar');
        me.fireEvent('customtoolbarevent',toolbar,true,{name:'demo'});
    }
});

Add the Custom Event in Controller

Sencha Touch controllers, provides control configuration which is used to map a Controller functions that should be called whenever certain view Component events are fired. This view Component must be specified using ComponentQuery Selectors. If you want to know the various ways for attaching event handler, Please read this Post.

As you may notice below, the fired custom event name 'loginButtonTap' is mapped to the onButtonLoginButtonTap() function for the toolbar component using componentQuery (#mytoolbar). The onButtonLoginButtonTap() function accepts the three parameters  button, isCustom, args which are passed from the fireEvent().
Ext.define('MyApp.controller.MyController', {
    extend: 'Ext.app.Controller',

    config: {
        control: {
            "#mytoolbar": {
                customtoolbarevent: 'onButtonLoginButtonTap'
            }
        }
    },

    onButtonLoginButtonTap: function(toolbar, isCustom, args) {
        Ext.Msg.alert("Fired Custom Event mapped with parameters");
    }
});

Output:

Hope, you enjoyed this Post.

10 comments:

  1. not working.event not firing !!

    ReplyDelete
    Replies
    1. Can you paste your code that will help me to find why event is not working from your end.

      Thanks

      Delete
  2. Could you please explain me to how to set default spinner value is 1 ? after that when we click on + button how it will automatically increase by 1?

    ReplyDelete
    Replies
    1. Anu, Please find the requested information in the below url

      http://sureshdotariya.blogspot.in/2013/09/sencha-touch-2-add-spinner-field-with.html

      Hope, this solves your problem.

      Thanks
      Suresh Ariya

      Delete
    2. Thank you..This is what exactly I need..really helpful your link..

      Delete
    3. I am a newer of sencha touch and mvc architecture so could you please explain me to the exact working process of mvc architecture in sencha touch?

      Delete
    4. how to do a expense tracking system using sencha touch 2.2.1?

      Delete
    5. Regarding MVC Architecture,
      M=> Model (here you will define table data column names (meta data)
      V=> View (here you will define HTML source i.e component, panel etc)
      C=> Controller (here you will map routing and attach views events to methods

      Additionnally Sencha has 'Store' concept which is used to store the model data. (similar to DB table). And view will use Store to display data in the frontend i.e component.

      For more information, please refer the below URL

      http://stackoverflow.com/questions/9229849/sencha-touch-2-0-mvc-tutorial

      Thanks
      Suresh Ariya

      Delete
    6. Hi, I am working on expense tracking system app for sencha touch. I will add a separate post in my blog in the upcoming week.

      Thanks.
      Suresh Ariya

      Delete