8

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) {...
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Leron
  • 9,546
  • 35
  • 156
  • 257

2 Answers2

20

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');
sha
  • 17,824
  • 5
  • 63
  • 98
  • Isn't it possible to work with `getController` to call the function? I've tried with `Ext.getController('name')` and `this.getController('name') and both gave me a mistake `getController is not a function` but I thought that this is the right way to go. – Leron Jun 18 '12 at 13:58
  • 1
    getController is a method from Ext.app.Controller class, so you need to have a reference to your current view's controller - which is again I think against MVC :) – sha Jun 18 '12 at 14:02
  • In fact let me explain -I have a form for mail notifications which should be shown on different occasions (from different views, on different events) but the form is defined in one controller. What do yo think is the best way to implement such a thing. Obviously copy-pasting the code is not the right solution, I think there should be a proper way to place the code on one place and use it from many but I'm not sure what exactly to do? – Leron Jun 18 '12 at 14:02
  • Why can't you just create a view from different places? Controller code will automatically pick it up. – sha Jun 18 '12 at 14:03
  • 1
    You don't need to create controller's instances manually. They got created by app class. – sha Jun 18 '12 at 14:08
  • I'm not exactly sure what you mean, but I have a function that is used to create the whole form and this functions is called by pressing a button, and the code itself is in the controller. I kinda have to deal with this because it's another's work and my task is to find a way to call this function *which aactually creates the editor) from everywhere. – Leron Jun 18 '12 at 14:09
  • Thanks don't know why it keeps saying that `getController is not a function`. `getStore` and `getView` works fine, guess I'm missing something. – Leron Jun 18 '12 at 14:35
  • but you're getting application instance? – sha Jun 18 '12 at 14:38
  • Sorry I think I don't get the whole idea here. I\ll post the more significant parts of my cone in the main topic. If you can please point my mistakes, Thinks I don't have to try this at the controller to start with but...see the code... – Leron Jun 18 '12 at 14:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12701/discussion-between-sha-and-leron) – sha Jun 18 '12 at 14:54
  • Probably worth updating this answer. In 4.2.x, the app is automatically set and you can access it using `MyApp.app`. – Evan Trimboli Aug 22 '13 at 23:23
5

You can use this -

this.getController('Controller Name').someFn();
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
Kanchan
  • 1,609
  • 3
  • 22
  • 37