Miguelio
Miguelio

Reputation: 29

Extjs 6 Modern - How to execute function in controller from view?

In controller

listen: {
   global: {
     'ontest': 'ontestfunction'
   }
},

ontestfunction: function(){
    alert('oks 31');
}

In view

listeners: {
                element: 'element',
                click: function(){
                    Ext.GlobalEvents.fireEvent('ontest');
                }
     }

It is the only way I've found to work, you know some other way?

Upvotes: 0

Views: 3077

Answers (2)

savi
savi

Reputation: 11

Since you're using EXT 6, I assume you're using viewcontrollers and not controllers.

Ext is going to resolve the scope as the view's controller automatically, no need to write any extra code.

First make sure you define the viewcontroller as the controller of the view:

controller: 'aliasofviewcontroller'

after that you can basically assign any of the controllers functions to the handlers of the components on the view.

Let's say you have the following function in your controller:

onClickCustomHandler:function(e){
...
}

Using the following syntax, the function is going to get called every time you're button is clicked:

Ext.create('Ext.Button', {
    text: 'My button',
    handler: 'onClickCustomHandler'
});

Or using xtype:

{
   xtype:'button',
   text:'My button',
   handler: 'onClickCustomHandler'
}

For further reading: http://docs.sencha.com/extjs/6.0.2/guides/application_architecture/view_controllers.html

Upvotes: 1

Alexander
Alexander

Reputation: 20224

You can get any controller using Ext.app.Controller.getController.

Since application is derived from controller, all you have to know is the name of your app and the desired controller; and calling the function is a piece of cake:

var configController = MyAppName.app.getController("MyAppName.controller.Configuration");
configController.ontestfunction();

Upvotes: 2

Related Questions