Results 1 to 2 of 2

Thread: When to use event listener vs a controller?

  1. #1
    Sencha User
    Join Date
    Apr 2012
    Location
    Billerica, MA
    Posts
    60
    Vote Rating
    10
      0  

    Default When to use event listener vs a controller?

    Hi all,

    I'm still fairly new to Sencha Touch, so please bear with me if this is a stupid question.

    It seems you can add behaviors to view components by either implementing a controller, or by adding an event listener right in the view component. The documentation explains how to do each, but doesn't really explain when you should use one over the other.

    Here's my concrete example: I am creating a subclass of Ext.tab.Panel that allows swiping between tabs. On the initialize event, I go through and add the swipe event listener to each item's element. There are two ways to do this (I've implemented both as a learning exercise):

    Approach 1: Event listener in the tabpanel subclass:
    Code:
    Ext.define("MyApp.view.SwipeTabPanel", {
      extend: 'Ext.tab.Panel',
      xtype: 'swipeTabPanel',
      config: {
        listeners: {
          initialize: function() {
              // do the work here
          }
       }
    });
    Approach 2: Use a controller
    View component:
    Code:
    Ext.define("MyApp.view.SwipeTabPanel", {
      extend: 'Ext.tab.Panel',
      xtype: 'swipeTabPanel'
    });
    Controller:
    Code:
    Ext.define('MyApp.controller.TabSwipe', {
        extend: 'Ext.app.Controller',
          config: {
          control: {
            'swipeTabPanel': {
              initialize: 'addSwipeListener'
            }
          }
        },
        addSwipeListener: function(comp, opts) {
           // do the work here
        }
    });
    Both of these work, but I'm not sure which is the "right way" to do it in Sencha Touch 2.

    Thanks in advance.

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Answers
    3931
    Vote Rating
    1272
      1  

    Default

    If you are using MVC then your logic should be within the controller. The only logic that should go within the view is if you need to add a listener to an element but that should just fire a custom event on the component so the controller can then listen to it and logic is in the controller.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •