1. #1
    Sencha User
    Join Date
    Apr 2012
    Location
    Billerica, MA
    Posts
    60
    Vote Rating
    9
    jattardi will become famous soon enough

      0  

    Default Unanswered: When to use event listener vs a controller?

    Unanswered: 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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,346
    Answers
    3541
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      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 @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 1

Tags for this Thread