2012-04-17 59 views
2

我試圖將ExtJS4應用程序拆分爲模塊。ExtJS4模塊化與獨立控制器?

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

的問題是,當我啓動應用程序,它的inits 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與創建()調用 Ping.js的文件是文件與定義( )調用

- 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 

inpu T:

MyController.create(); 

輸出:

constructor 

- EDIT3 -

創建時,控制器需要在配置對象的應用程序對象。應用程序對象將自身添加到所有控制器,這些控制器是用手工創建的而不是。它調用如下所示:

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

後來它使用應用程序對象將控制調用重定向到它。

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

因此,我可能會實施一些機制,當我創建它們時,會自動將這些應用程序添加到這些控制器。

+1

你是什麼意思'控制器沒有控制()功能?你能發佈一些你的代碼嗎? – sha 2012-04-17 17:15:39

+0

錯誤消息不幫助我。控制被稱爲這個,但它說這是未定義的...笏?! – 2012-04-18 09:06:06

+0

恭喜發現解決方案!可能它會依靠你的首選方法來組織源代碼:)我發現ExtJs依賴於目錄結構在幾個地方,並決定不發明任何東西,並按照他們的建議組織事情。 – sha 2012-04-18 18:22:25

回答

1

你試過

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

凡申請是Ext.application.launch期間創建的對象的引用 - 如

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

這個也工作了,謝謝!它甚至比我的解決方案更好,因爲我不必攜帶應用程序對象。 – 2012-04-19 08:42:34

+0

在相應視圖中調用它是個好主意嗎?所以主模塊控制器只會在視圖需要時加載。 – 2012-04-19 10:45:48

+0

哪個視圖?我不確定ExtJS在創建視圖時的行爲,然後再創建控制器,而不是控制該視圖。我通常創建控制器(C),然後立即創建(C)控制的視圖。 – 2012-04-19 16:07:11

0

不需要手動撥打init()。只需撥打Ext.create即可自動調用構造函數。

+0

我將一個log()放入init()。如果我手動調用它,我會得到日誌輸出和提到的錯誤。如果我只是調用create(),我根本得不到輸出。 – 2012-04-18 14:33:00

+0

即使你像這樣調用它:'c = Ext.create('App.module.ping.controller.Ping',{});'? – sha 2012-04-18 14:43:08

+0

是的。我調試了create()調用中發生的所有事情,並且從未進入init()。也許這是ExtJS 4.1的行爲? – 2012-04-18 14:53:44

1

爲了誰比誰來這裏尋找一個解決方案:

Uncaught TypeError: Cannot call method 'control' of undefined 

一天長沮喪撇開,上面的答案沒有解決我的問題,b UT導致我嘗試以下,這根本解決問題:

// 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(); 
      } 
     } 
    }); 
}, 

相應的應用變量是一樣的「VAR應用= {}」這是在問題的選擇答案 - 這意味着我沒有」不需要將其添加到全局(或根本)。我一定曾嘗試過一百萬種不同的東西,但最終這一切都奏效了。希望它能拯救別人。

+0

哈哈,老問題:我後來改用了DeftJS,因爲他們的視圖控制器的概念更適合我的模塊系統。 – 2013-10-25 16:30:36

+0

很高興知道!礦井更像是一個動態控制器陷入泥潭和奇怪的情況。這是我發現的與我的問題類似的唯一帖子:( – 2013-10-25 18:53:40