ExtJS 4 - how to call function from a certain controller in different views

Leron_says_get_back_Monica picture Leron_says_get_back_Monica · Jun 18, 2012 · Viewed 26.5k times · Source

I have a function which is described in one of my controllers. The function takes care of creating a form that I need to use in different cases, from different views. Is it possible and what is the way to call this function from the views that I need without adding the same code in each controller.

Here is the code of the controller where I try to use a method from other controller:

Ext.define('MY.controller.EventsController', {
    extend: 'Ext.app.Controller',
    models: [
        'EventsRecord'],

    stores: [
        'Events'],

    views: [
        'EventsGrid'],
    refs: [{
        ref: 'EventsGrid',
        selector: 'CalendarEvent'
    }],

    init: function () {
        this.control({
            'CalendarEvent': {
                afterEditFinish: this.askForNotify,
                deleteEvent: this.deleteEvent,
                calendarEditFunc: this.calendarEditFunc,
                addCalendarEvent: this.addCalendarEvent,
                itemclick: this.onSelectEnableBtn
            }
        })

    },

Here I try to use something like var contr = Ext.getController('SomeController');and..nothing..

askForNotify: function(editor, e) {...

Answer

sha picture sha · Jun 18, 2012

Make it part of global class with singleton: true and access it from anywhere in your code. Just calling controllers methods from views is kind of against MVC paradigm...

Update: If you really can't change existing code - do the following.

Save off reference to the your app somewhere (presumable you have your application defined something like that:

Ext.application({

   launch: function() {
      _myAppGlobal = this;
   }
});

Use this variable to get controller you want:

_myAppGlobal.getController('MyController');