简体   繁体   English

如何在extjs中将args传递给构造函数?

[英]how to get args to constructor at extjs?

I have a some class 我上课

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);
}});

when I create an object var AddOrgWindowForm = new AddOrgWindowUI('aaa'); 当我创建一个对象时var AddOrgWindowForm = new AddOrgWindowUI('aaa'); I want to get arg ('aaa') to my new form value (value m). 我想将arg('aaa')设置为新的表单值(值m)。 How get it? 怎么得到的? Im trying initComponent: function(m) { and thats not working. 我正在尝试initComponent: function(m) {而那是行不通的。

The initComponent function is called internally on one of the base classes of Ext.Window . Ext.Window的基类之一上内部调用initComponent函数。 You shouldn't try to call it directly. 您不应该尝试直接调用它。 That is why it won't handle your own parameters. 这就是为什么它不会处理您自己的参数的原因。

So I recommend you to use the standard form parameters when extending ExtJS classes . 因此,我建议您在扩展ExtJS类时使用标准格式参数。

It is as simple as initializing the object with the property or methods you want to override (or insert in case the property is not in there already). 就像使用要覆盖的属性或方法初始化对象一样简单(或在属性不存在的情况下插入)。 And then just using the this keyword to access them. 然后只需使用this关键字即可访问它们。

This is possible because for every Ext.Component and its subclasses, the first parameter passed to the constructor should be an object, and every member in that object will be copied to the new object constructed. 这是可能的,因为对于每个Ext.Component及其子类,传递给构造函数的第一个参数应该是一个对象,并且该对象中的每个成员都将被复制到构造的新对象中。 And most ExtJS classes extend directly or indirectly from Ext.Component , and you are extending from Ext.Window which extends from Ext.Component too. 大多数ExtJS类都直接或间接地从Ext.Component扩展,而您也从Ext.Window扩展,而Ext.Window也从Ext.Component扩展。

Here you have your example fixed: 在这里,您已修复示例:

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();
}

pass m as an arg of initComponent: 将m作为initComponent的arg传递:

edit: 编辑:

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);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM