ExtJS 4.2.1 - 无法从控制器获取View实例

时间:2013-07-31 15:27:33

标签: extjs extjs4 extjs4.2 extjs-mvc

在我的应用程序中,我的视口有一个项目,一个主视图,它是一个从Ext.container.Container扩展的简单类。

我也有一个主控制器,我正在尝试获取视图实例,所以动态我可以在用户登录时推送相应的项目。

我尝试过使用视图:['MainView'],参考[{selector:'thextype'}]等,没有运气。

我在sencha touch中使用引用(ref)来做这类事情,你能帮我解决Extjs v4.2吗?

为了澄清,我不是想要获取DOM元素,我正在尝试使用相关方法获取视图实例。

提前致谢,

3 个答案:

答案 0 :(得分:4)

定义视图的xtype:

xtype: 'mainview'

然后在你的控制器中:

requires: ['...'] // your view class
// ...
refs: [{
    ref: 'mainView',
    selector: 'mainview' // your xtype
}]

然后您可以使用this.getMainView()

在控制器中获取视图的实例

答案 1 :(得分:1)

我试过没有好结果。 我想要做的就像是。根据你的回答应该工作

Ext.define('MyApp.view.MainView', {
extend: 'Ext.container.Container',
alias: 'widget.mainContainer',
cls: ['mainContainer'],
items: [
    {
        xtype: 'panel',
        items: [
            {
                html: "my view"
            }
        ]
    }
]});

Ext.define('MyApp.controller.MainController', {
extend: 'MyApp.controller.BaseController',
refs: [
    {
        ref: 'mainContainer',
        selector: 'mainContainer'
    }
],

init: function() {
    this.getApplication().on({
        openDashboard: this.onOpenDashboard
    });
},
onOpenDashboard: function() {
    var mainContainerView = this.getMainContainer();
    mainContainerView.showSomething(); //mainContainerView should be an instance of the view.
}});

其中,如果登录成功后会触发openDashboard事件。

答案 2 :(得分:1)

经过一段时间的调试后,似乎问题就是它被称为函数的上下文。

我在init方法中添加了一行:

var openCrmDashboardFn = Ext.Function.bind(this.onOpenCrmDashboard, this);

并且有效。

谢谢!