extjs如何参数化组件

时间:2019-05-27 21:18:58

标签: extjs

我正在尝试将两个参数oneOfAlltwoOfAll传递到容器中。通过initComponent函数,我可以做到:

initComponent: function () {
       this.callParent(arguments);
       console.log('Results', this.oneOfAll, this.twoOfAll);
}

,并且在以下情况下,浏览器的控制台会打印正确的数据

items: [
          {
              xtype: 'button',
              text: 'Other click',
              id: 'buttonClick',
              handler: function(item) {
                  Ext.Msg.alert('Other Click', '!!!' + this.oneOfAll);
              }
          },
          {
              xtype: 'button',
              text: 'CLICK',
              id: 'buttonClick_2',
              handler: function(item) {
                 Ext.Msg.alert('Other Click', '!!!' + this.twoOfAll);
              }
           }
      ]

在两次返回undefined时,容器配置都看不到这些值。

我有这个小提琴:https://fiddle.sencha.com/#view/editor&fiddle/2sk3

如何将这些参数传递到容器中并被容器看到?

3 个答案:

答案 0 :(得分:1)

在处理程序中,this是指项目对象,而不是您的组件。

要解决此问题,您可以尝试按ID访问组件,然后直接获取其属性。

例如(使用您的小提琴中的mypanel ID):

{
    xtype: 'button',
    text: 'Other click',
    id: 'buttonClick',
    handler: function (item) {
        var cmp = Ext.getCmp('mypanel');
        Ext.Msg.alert('Other Click', '~~!!' + cmp.oneOfAll);
    }
},

答案 1 :(得分:1)

您可以通过initComponent方法设置对当前组件的引用,并在子组件中使用它们。

看一个例子: https://fiddle.sencha.com/#view/editor&fiddle/2skh

答案 2 :(得分:1)

您的处理程序具有不同的作用域,并且您设置的配置也不同。

为了获取值,请先获取该组件的引用,然后尝试访问。

handler: function(item) {       
   Ext.Msg.alert('Other Click', '!!!' + item.up().oneOfAll);
   Ext.Msg.alert('Other Click', '!!!' + item.up().twoOfAll);
}
相关问题