【问题标题】:Cannot access controllers from outside object无法从外部对象访问控制器
【发布时间】:2017-03-13 19:04:14
【问题描述】:

我这里有一些简单的代码。它创建了一个名为“MyApp”的应用程序,并定义了一个名为“MyController”的控制器。 MyController 然后作为配置传入应用程序。

            function onDeviceReady() {

                alert('In onDeviceReady() function');

                MyApp.app.getController('MyController').somefunction();
//                MyApp.controller.MyController.somefunction()
            }

            Ext.application({
                name: 'MyApp',
                controllers: ['MyController']
            });

            Ext.define( 'MyApp.controller.MyController', {
                extend: 'Ext.app.Controller',

                init: function () {
                    alert('Login controller');
                    onDeviceReady();
                },

                somefunction:function(){
                    alert ( 'some function callled' )
                }
            });

到目前为止一切顺利。典型的标准东西。现在,在从 MyController 调用“init”之后,我调用了一些外部方法 onDevideReady。通过这种方法,我想再次访问我的控制器。

我已经尝试过:

  • MyApp.app.getController('MyController').somefunction();
  • MyApp.controller.MyController.somefunction()

但是这些东西似乎都不起作用......我也在使用 Extjs 4.1,这可能是一个问题。基本上我想以某种方式从我的代码中访问我的控制器(不一定来自另一个控制器)

这是问题的关键:

https://fiddle.sencha.com/#fiddle/1jjh

【问题讨论】:

    标签: extjs extjs4.1


    【解决方案1】:

    对我来说没问题(一旦我移动你的控制器/应用程序)。

    function onDeviceReady() {
        MyApp.app.getController('MyController').somefunction();
    }
    
    Ext.define('MyApp.controller.MyController', {
        extend: 'Ext.app.Controller',
    
        init: function() {
            onDeviceReady();
        },
    
        somefunction: function() {
            alert('some function callled')
        }
    });
    
    Ext.application({
        name: 'MyApp',
        controllers: ['MyController']
    });
    

    https://fiddle.sencha.com/#fiddle/1jk7

    然而,这并不是我做事的方式。我宁愿没有一个全局函数,而是让它深入我的应用程序。我宁愿有一个我会触发事件的单例类,这样控制器就会监听这些事件。另外,下车 4.1 :)

    【讨论】:

    • 是的,我很想摆脱 Extjs 4。5+ 中的 MVC 架构要好得多:)。 4 中的 MVC 架构真的很糟糕。
    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多