【问题标题】:ExtJs - how to set id of component when using multiple instancesExtJs - 使用多个实例时如何设置组件的id
【发布时间】:2012-06-10 06:29:49
【问题描述】:

我使用 ExtJs 4.1 和 DeftJs。

当同时使用相同视图的多个实例时,解决给定id: 'abc' 的组件时会出现问题,因为所有实例都将具有相同的 ID。

那么如何命名 id 以防止这种情况发生?

一种选择是动态创建组件。

我更喜欢的另一个选项是仅通过特定视图的 id 调用组件,例如:

this.getView.getComponent('x').getId('abc')

【问题讨论】:

    标签: javascript model-view-controller extjs


    【解决方案1】:

    我尽量避免使用全局 ID,并使用控制器中的 this.getView().getComponent() 来访问我的视图。如有必要,我还会在我的 id 名称中使用唯一的字符串。

    【讨论】:

      【解决方案2】:

      如果您可以将父元素设置为唯一 id,则可以在子项上使用 itemId 属性。 sencha 文档中的示例(取自它下面的链接):

      var c = new Ext.panel.Panel({ //
      height: 300,
      renderTo: document.body,
      layout: 'auto',
      items: [
          {
              itemId: 'p1',
              title: 'Panel 1',
              height: 150
          },
          {
              itemId: 'p2',
              title: 'Panel 2',
              height: 150
          }
      ]
      })
      p1 = c.getComponent('p1'); // not the same as Ext.getCmp()
      p2 = p1.ownerCt.getComponent('p2'); // reference via a sibling
      

      http://docs.sencha.com/ext-js/4-0/#!/api/Ext.AbstractComponent-cfg-itemId

      【讨论】:

      • 谢谢,但这与 mvc 和同一视图/控制器的许多实例无关,因为我无法在这样的视图中设置唯一 ID。我想通过它的控制器来处理特定的视图。
      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2010-12-14
      • 2011-12-15
      相关资源
      最近更新 更多