【问题标题】:ExtJS 4.2.1 - Cannot get View instance from the controllerExtJS 4.2.1 - 无法从控制器获取视图实例
【发布时间】:2013-07-31 15:27:32
【问题描述】:

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

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

我尝试过使用视图:['MainView']、refs[ { selector: 'thextype' }] 等,但没有成功。

我在 sencha touch 中使用参考(ref)来做这种事情,你能帮我用 Extjs v4.2 吗?

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

提前致谢,

【问题讨论】:

    标签: extjs extjs4 extjs4.2 extjs-mvc


    【解决方案1】:

    定义视图的 xtype:

    xtype: 'mainview'
    

    然后在你的控制器中:

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

    然后您可以使用this.getMainView() 在控制器中获取视图实例

    【讨论】:

      【解决方案2】:

      我试过了,没有好的结果。 我想做的是类似的事情。根据您的回复应该可以工作

      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 事件。

      【讨论】:

        【解决方案3】:

        经过一段时间的调试,问题似乎出在调用函数的上下文。

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

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

        它成功了。

        谢谢!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-02
          • 2014-11-07
          • 1970-01-01
          • 1970-01-01
          • 2013-02-26
          • 2023-03-14
          相关资源
          最近更新 更多