参考自动实例化的Sencha Touch 2视图

Att*_* O. 0 extjs sencha-touch extjs-mvc

我想从我的Sencha Touch 2控制器中获取我的视图参考,它看起来像这样(CoffeeScript):

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'
Run Code Online (Sandbox Code Playgroud)

出现"Hello,Books"内容(例如,呈现组件),但甚至从不调用呈现控件处理程序.我在这做错了什么?

Abd*_*oof 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();  
    }
});
Run Code Online (Sandbox Code Playgroud)

注意别名属性.这有助于您获取视图实例的引用.当您引用视图时.现在在你的控制器中你有:

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

    },
    .
    ...
Run Code Online (Sandbox Code Playgroud)

请注意,您没有使用id属性引用视图,而是通过别名或xtype引用视图.现在,如果您需要访问控制器中的其他控制器视图或任何其他组件,您还可以使用可用的refs系统.在这种情况下,您将在控制器定义中将refs数组定义为:

refs: [
    {ref: 'centerPane', selector: 'centerpane'}
],
Run Code Online (Sandbox Code Playgroud)

这里选择器属性是传递Ext.ComponentQuery给获取组件的值.因此,您可以通过以下方式获取centerpane的实例:

this.getCenterPane();
Run Code Online (Sandbox Code Playgroud)