dowjones123
dowjones123

Reputation: 3837

Where to use listeners and where to use controller - Sencha Touch 2

I am confused between the proper usage of Listeners vs Controllers

E.g. for the same button, I can make the handler for the button-tap event in the button description itself as :

Ext.Viewport.add({
    xtype: 'button',
    centered: true,
    text: 'My Button',

    listeners: {
        tap: function() {
            alert("You tapped me");
        }
    }
});

and also as in a separate controller as below.

Ext.define("NotesApp.controller.Notes", {
    extend: "Ext.app.Controller",
    config: {
        refs: {
            newNoteBtn: Get reference to button here
        },
        control: {
            newNoteBtn: {
                tap: "onNewNote"
            }
        }
    },
    onNewNote: function () {
        console.log("onNewNote");
    }


});

What is the best practice, and are there trade-offs?

Thanks

Upvotes: 0

Views: 232

Answers (1)

Saki
Saki

Reputation: 5856

To controller or not to controller, that is the question.

Well, technically, nothing would prevent you from doing one or the other. I have established a way how to decide for myself:

  1. I install listeners on view if the job they are doing does not cross boundaries of the view. If we take form as an example, disabling and enabling, showing and hiding of fields, if it depends only on the state of the form stay within the form - no controller.
  2. I delegate the logic to a controller if the action in one view (a button click, for example) influences another view or the whole application.

Again, these are my preferences, you can have another.

Upvotes: 1

Related Questions