2011-10-17 20 views
0

我想從我的煎茶觸摸控制器2,它看起來像這樣(的CoffeeScript),以我的觀點的引用:獲得一個refernce來自動實例化的煎茶觸摸2視圖

Ext.define 'MyApp.view.Books', 
    extend: 'Ext.Panel' 
    id: 'books' 
    config: 
     html: 'Hello, Books!' 
Ext.define 'MyApp.controller.Books', 
    extend: 'Ext.app.Controller' 
    views: [ 
     'Books' 
    ] 
    init: -> 
     @control 
      '#books': 
       render: -> 
        console.log 'This is never called. Why?' 
       show: -> 
        console.log 'This is called twice.' 
        # @getBooksView() is not a view instance, it is a class. Why? 
        console.log 'View class: ' + @getBooksView()?.$isClass 
Ext.application 
    name: 'MyApp' 
    controllers: [ 
     'Books' 
    ] 
    launch: -> 
     Ext.Viewport.setActiveItem 
      xtype: 'booksview' 

的「你好,Books的內容出現了(例如組件被渲染),但是渲染控制處理程序甚至從未被調用過。我在這裏做錯了什麼?

回答

5

對於任何類,您都不應使用硬編碼id!您應該將您的觀點編入爲:

Ext.define('MyApp.view.Books',{ 
    extend: 'Ext.Panel', 
    alias : 'widget.books', 
    config: { 
     html: 'Hello, Books!' 
    }, 
    initialize : function() { 
     // some init code etc.. 
     this.callParent(); 
    } 
}); 

請注意別名屬性。這有助於您獲取視圖實例的參考。當你參考視圖。現在,在你的控制器,你有:

... 
this.control({ 
    'books' : {  
     render: function() { 
      console.log('Render method called!'); 
     }, 
     show: function() { 
      console.log('Show method called!'); 
     } 

    }, 
    . 
    ... 

注意,你是不是用id屬性闖民宅你的觀點,而不是通過它的別名或的xtype。現在,如果您需要訪問其他控制器的視圖或控制器中的任何其他組件,還可以使用可用的參考系統。在這種情況下,你會在控制器定義界定裁判陣列:

refs: [ 
    {ref: 'centerPane', selector: 'centerpane'} 
], 

這裏選擇屬性是傳遞給Ext.ComponentQuery獲得該組件的值。所以,你可以通過以下方式獲得中間層實例:

this.getCenterPane(); 
+0

謝謝,這是一個非常好的解釋!最近我也認爲我不應該使用ID,所以我不再使用它們,但我仍然不知道如何獲取引用,所以我開始使用'name:'屬性和'.down('[name = .. 。]')',這顯然不是它的意圖。再次感謝您的出色答案。 –

+0

我也一直在爲此苦苦掙扎,示例程序似乎完全是人爲設計的,並且從來沒有顯示如何實際獲得自動實例化視圖。很好的解釋 –