使用独立控制器进行ExtJS4模块化?

时间:2012-04-17 15:36:16

标签: model-view-controller extjs extjs-mvc

我正在尝试将ExtJS4应用程序拆分为模块。

 - app
  - store
  - controller
  - model
  - view
  - module
     - m1
         - model
         - view
         - controller
     - m2
         - model
         - ...

问题是,当我启动应用程序并且它在一个m1控制器中时,控制器没有this.control()函数。

- 编辑 -

我在控制器文件夹中定义了一个类。

Ext.define( 'App.module.ping.controller.Ping', {
  extend: 'Ext.app.Controller',

  requires: [
     'App.module.ping.view.PingPanel'
  ],

  init: function() {
     this.control( {
        '#app.module.ping': {
           render: this.onPanelRendered
        }
     } );
  },

  onPanelRendered: function() {
     ...
  }

});

后来我打电话给

Ext.create('App.module.ping.controller.Ping').init();

但是我收到了这个错误:

Uncaught TypeError: Cannot call method 'control' of undefined
Ext.define.control./lib/extjs-4.1.0/src/app/Controller.js:414
Ext.define.init./app/module/ping/app/controller/Ping.js:11
Ext.define.init./app/module/ping/app/Module.js:11
(anonymous function)app.js:17
(anonymous function)app.js:35

Module.js是带有create()调用的文件 Ping.js是带有define()调用的文件

- edit2 -

病理学例子:

输入:

Ext.define( 'MyController', { 
  extend: 'Ext.app.Controller', 
  init: function() { console.log('initialized'); } 
});

输出:

function constructor() {
  return this.constructor.apply(this, arguments);
}

输入:

Ext.create('MyController');

输出:

constructor

输入:

MyController.create();

输出:

constructor

- edit3 -

控制器在创建时需要配置对象中的应用程序对象。应用程序对象将自身添加到所有控制器,这些控制器是使用create手动创建的。它叫做这样的事情:

Ext.create('App.controller.Users', { application: this, ... } );

稍后它使用应用程序对象将控制调用重定向到它。

control: function ( config ) { this.application.control( config ) };

所以我可能会实现一些机制,当我创建它们时会自动将应用程序添加到这些控制器。

3 个答案:

答案 0 :(得分:1)

你试过吗

var pingController = Application.getController('App.module.ping.controller.Ping');
pingController.init(); //or pingController.init(Application);

其中Application是对Ext.application.launch期间创建的对象的引用 - 例如

var Application = {};
Ext.application({
    //all of your settings,
    launch:function(){
         Application = this;
         //other launch code
    }
}
});

答案 1 :(得分:1)

来到这里寻找解决方案的其他人:

Uncaught TypeError: Cannot call method 'control' of undefined

除了一天的挫折之外,上面的答案并没有解决我的问题,但引导我尝试以下方法,这确实解决了这个问题:

// app.js
Ext.application({
    ...

    launch: function(){
        var me = this;
        ...

        me.getController('ControllerName');
    }
});

// inside controller/ControllerName.js
init: function(app){
    var me = this;

    app.control({
        '#editButton': {
            click: function(){
                me.someFunction();
            }
        }
    });
},

该app变量与问题所选答案中的“var Application = {}”相同 - 意味着我不需要全局添加(或根本不需要)。我一定试过了一百万种不同的东西,但这终于奏效了。希望能拯救别人。

答案 2 :(得分:0)

无需手动调用init()。只需调用Ext.create,就会自动调用构造函数。