【问题标题】:how to get args to constructor at extjs?如何在 extjs 中获取 args 到构造函数?
【发布时间】:2010-09-02 13:11:10
【问题描述】:

我有一门课

AddOrgWindowUI = Ext.extend(Ext.Window, {
title: 'form',
width: 400,
height: 198,
layout: 'form',
padding: 5,
initComponent: function() {
    this.items = [
        {
            xtype: 'textfield',
            fieldLabel: 'parapapa',
            anchor: '95%',
            value: m,
            emptyText: 'perapapa'
        }
    ];
    AddOrgWindowUI.superclass.initComponent.call(this);
}});

当我创建一个对象var AddOrgWindowForm = new AddOrgWindowUI('aaa'); 时,我想将 arg ('aaa') 设置为我的新表单值(值 m)。怎么得到? 我正在尝试 initComponent: function(m) { 但这不起作用。

【问题讨论】:

    标签: javascript extjs arguments


    【解决方案1】:

    initComponent 函数在内部调用 Ext.Window 的基类之一。您不应该尝试直接调用它。这就是为什么它不会处理您自己的参数。

    所以我建议你extending ExtJS classes时使用标准的表单参数。

    这就像使用您要覆盖的属性或方法初始化对象一样简单(或插入以防该属性已不存在)。然后只需使用this 关键字即可访问它们。

    这是可能的,因为对于每个Ext.Component 及其子类,传递给构造函数的第一个参数应该是一个对象,并且该对象中的每个成员都将被复制到构造的新对象中。而且大多数 ExtJS 类直接或间接地从 Ext.Component 扩展而来,而您也从 Ext.Window 扩展而来,而 Ext.Component 也是从 Ext.Component 扩展而来的。

    这里你的例子已经修复了:

    var AddOrgWindowUI = Ext.extend(Ext.Window, {
        title: 'form',
        width: 400,
        height: 198,
        layout: 'form',
        padding: 5,
    
        initComponent: function() {
            this.items = [
                {
                    xtype: 'textfield',
                    fieldLabel: 'parapapa',
                    anchor: '95%',
                    value: this.initialValue,
                    emptyText: 'perapapa'
                }
            ];
            AddOrgWindowUI.superclass.initComponent.call(this);
        }
    });
    
    function test() {
        var AddOrgWindowForm = new AddOrgWindowUI({initialValue:'aaa'});
        AddOrgWindowForm.show();
    }
    

    【讨论】:

      【解决方案2】:

      将 m 作为 initComponent 的 arg 传递:

      编辑:

      AddOrgWindowUI = function(input) {
          var m = input;
          return Ext.extend(Ext.Window, {
              title: 'form',
              width: 400,
              height: 198,
              layout: 'form',
              padding: 5,
              initComponent: function() {
                  this.items = [
              {
                  xtype: 'textfield',
                  fieldLabel: 'parapapa',
                  anchor: '95%',
                  value: m,
                  emptyText: 'perapapa'
              }
          ];
                  AddOrgWindowUI.superclass.initComponent.call(this);
              }
          });
      }
      

      【讨论】:

      • 谢谢,但是如何声明一个新对象呢? var AddOrgWindowForm = new AddOrgWindowUI.initComponent('aaa'); - 不工作。
      • 它也不起作用 :( 试试看。Firebug 说 - “AddOrgWindowUI.initComponent 不是函数”
      • 这样做你失去了将任何其他配置项传递给构造函数的能力。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多