获取对自动实例化 Sencha Touch 2 视图的引用

发布于 2024-12-10 19:54:27 字数 916 浏览 0 评论 0原文

我想从我的 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'

出现“Hello, Books”内容(例如,渲染组件),但渲染控制处理程序永远不会出现叫。我在这里做错了什么?

I'd like to get a reference to my view from my Sencha Touch 2 controller, which looks like this (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'

The 'Hello, Books' content appears (e.g. the component is rendered), but the render control handler is never even called. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-17 19:54:27

您不应该对任何类使用硬编码的 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 系统。在这种情况下,您将在控制器定义中将 refs 数组定义为:

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

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

this.getCenterPane();

You shouln't use hardcoded id for any of the classes! You should be codding your view as:

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

Note the alias property. This helps you to get the reference of your view's instances. When you refer the view. Now in your controller you have:

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

    },
    .
    ...

Note that you are not refering your view with the id property, instead through its alias or xtype. Now, if you need to access other controller's views or any other component in your controller, you can also make use of the refs system available. In this case, you will define the refs array in the controller definition as:

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

Here the selector property is the value passed to Ext.ComponentQuery to get the component. So, you can get the instance of centerpane by:

this.getCenterPane();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文