0

I've been using ExtJS for awhile but am now trying to convert my projects to properly use the MVC style they recommend.

I have a view and a controller. I'm trying to declare an event listener for boxready to call a controller method. This is not working when I declare the listener inside initComponent - it says it can't find the method in my view when I'm expecting it to look for the method in my controller.

See jsFiddle here for example (relevant code is in MyPanel.js): senchafiddle

Tarabass
  • 3,132
  • 2
  • 17
  • 35
zeke
  • 3,603
  • 2
  • 26
  • 41

2 Answers2

1

There is no need to attach the listener inside initComponent, do it declaratively:

listeners: {
     boxready: 'controllerMethodName'
}

Declare this directly on your view.

Brandon Smith
  • 1,197
  • 9
  • 13
1

You could do it like Brandon proposed. I think this is also what Sencha has in it's documentation.

I am doing it a little bit different, because i don't what the view to know about the controller. Regarding your example, meaning: The view should not know that there is or should be a test() function inside the controller. Take a look at the init() inside the controller:

Ext.define('MyApp.view.MyPanelController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.mypanel',

    init: function () {
        this.getView().on("boxready",this.test, this);
    },

    test: function () {

        Ext.Msg.alert('hi');
    }
});

And very little in the View:

Ext.define('MyApp.view.MyPanel', {
    extend: 'Ext.form.Panel',
    xtype: 'mypanel',
    controller: 'mypanel',

    html: 'sometext',
    title: 'sometitle'
});

This could have some downsides that i am not aware of.

Check out this corresponding sfiddle: https://fiddle.sencha.com/#fiddle/tpm

Lucian Depold
  • 1,999
  • 2
  • 14
  • 25
  • Arguably, the VC and the view are by nature, coupled. So I don't think it makes sense to do it this way. – Evan Trimboli Sep 12 '15 at 12:28
  • Yes, they are connected by the "controller" attribute. But i am not getting why the View should know about the controller's functions. It's the job of the controller to act upon actions triggered by the view and then change the view accordingly. If the View tells the controller with what function to react on an event, then the View controls the Controller. – Lucian Depold Sep 12 '15 at 21:31
  • The view doesn't know about the controller implementation, only that there is an interface available to delegate to. This coupling is lose and you could easily change controllers by simply implementing the same methods. – Brandon Smith Sep 15 '15 at 11:43