如何在 EXTJS 中获取 MAIN 控制器的引用?

How to get reference of MAIN controller in EXTJS?

如何在 EXTJS 中获取控制器的引用?

Sencha 模板为现代应用程序生成主视图:

Ext.define('MyApp.view.main.Main', {
    extend: 'Ext.navigation.View',

    xtype: 'main',
    id: 'main',

    controller: 'main',

    platformConfig: {
        phone: {
            controller: 'phone-main'
        }
    },
    
... 

Ext.define('MyApp.view.main.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

首先我不明白的是有两个控制器(mainphone-main)。我想 phone-main 顾名思义就是 phone。但是现代工具包不适合 phone 吗?在这两个容器中的哪个容器中放置用户定义的函数?

无论如何,我在 main 控制器中有一些函数,我想获得该控制器的引用,因为我想从另一个控制器调用这些函数。我试图获得主控制器的参考,如:

aa = Ext.getCmp('main');
ref = aa.getController();

但这让我参考了 phone-main 而不是 main 控制器。

我也试过:

ref = MyApp.app.getController('main');

但是,这根本不起作用。同样适用于:

ref = MyApp.app.getController('MyApp.view.main.MainController');

在此示例中,根据设备(phone 或桌面)适当 ViewController 将附加:

Ext.define('MyApp.view.main.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',
    fooProperty: 'fooMainValue'

});
Ext.define('MyApp.view.main.PhoneMainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.phone-main',
    fooProperty: 'fooPhoneValue'

});

Ext.define('MyApp.view.main.Main', {
    extend: 'Ext.navigation.View',

    xtype: 'main',
    id: 'main',
    controller: 'main',
    platformConfig: {
        phone: {
            controller: 'phone-main'
        }
    },
    items: [{
        title: 'First',
        items: [{
            xtype: 'button',
            text: 'Click me',
            handler: function () {
                console.log(this.up('main').getController().fooProperty);
            }
        }]
    }]
});

Ext.create('MyApp.view.main.Main', {
    renderTo: document.body,
    height: 200
});

只需单击 chrome 开发人员工具中的“切换设备工具栏”,根据设备将被打印 'fooMainValue' 或 'fooPhoneValue' 属性。更多阅读 here。 “在这两个容器中的哪个容器中放置用户定义的函数?” => 这意味着您可以为 phone 和桌面设备使用两个不同的控制器,或者您可以创建一个基础 class ViewController 并使用自定义逻辑扩展桌面和 phone 控制器设备。常规 methods/properties 将放置在父(基)控制器中,自定义放置在子控制器中 classes.

“但是现代工具包不适合 phone 吗?” => 嗯..据我所知,他们试图开发一些新东西..阅读前内部人士的this