向组件添加功能

时间:2014-08-04 11:38:44

标签: extjs extjs3

我看到以下代码(ExtJs 3.0),并且有疑问。

Ext.ns('Dummy.');
Dummy.RevEvents = Ext.extend(Ext.Panel, 
{
    getData:function(){
    this.deletedRows=[];
    this.newRows=[];
    ......  
    },
    deletedRows:[],
    newRows:[],
    addEditableRow: function(){
    ...
    },
    initComponent:function(){   
        var revEvents_config = {
            autoScroll:true         
             ...
             ,listeners: {afterrender: function(t) {    
            ...
            }}
            ,buttons:[              
            ...
            ]
        }
    Ext.apply(this, Ext.apply(this.initialConfig, revEvents_config));
    Dummy.RevEvents.superclass.initComponent.call(this, revEvents_config);
    },..    
});
Ext.reg('REVEVTS', Dummy.RevEvents);

1.将配置传递给超类的init组件的意图是什么?
尚未将其应用于申请?
2.建议将侦听器和控件添加到组件中吗?

2 个答案:

答案 0 :(得分:0)

代码可能是由一个不太了解发生了什么的人编写的。

Ext.apply(this, Ext.apply(this.initialConfig, revEvents_config));

这将#1 /使用组件的默认设置initialConfig覆盖组件revEvents_config,然后#2 /将结果配置应用到组件上。我假设开发人员改变了预期用途:#1 / initialConfig是只读的,而#2 /使用某些默认设置覆盖组件的配置是没有意义的。

在Ext 3中将侦听器添加到扩展对象的标准方法是执行以下操作:

/**
 * @class MyNamespace.MyComponent
 * @extends OtherNamespace.OtherComponent
 * <p>Some comments here to be built by jsduck</p>
 */
MyNamespace.MyComponent = Ext.extend(OtherNamespace.OtherComponent, {
    defaultProp1 : defaultVal1,
    defaultProp2 : defaultVal2,

    // we get here AFTER the component's properties have been initialized by
    // the Ext.extend method.
    initComponent : function(){
        // call OtherNamespace.OtherComponent's initComponent
        MyNamespace.MyComponent.superclass.initComponent.call(this);
        // add custom events to the existing ones from OtherNamespace.OtherComponent
        this.addEvents('event1', 'event2', ...);
        // add any logic pertaining to the resulting config from Ext.extend
        if(this.defaultProp1 === ...){ ... }
    },

    otherPublicMethod: function(){
        ...
    },

    otherPublicMethodThatOverridesSuperClass: function(){
        // do we need to call the super method?
        // if yes:
        MyNamespace.MyComponent.superclass.otherPublicMethodThatOverridesSuperClass.call(this);
        ...
    }

});
Ext.reg('mycustomtype', MyNamespace.MyComponent);

答案 1 :(得分:0)

  1. 不确定意图是什么,将配置传递给initComponent将不会做任何事情。小组的initComponent没有任何论据。与initialConfig混淆是要求未定义的行为。默认的Component构造函数将接受传入的配置,将其应用于当前实例并将其保存为initialConfig。修改initialConfig或甚至查看其值几乎没有任何参数。

  2. 添加侦听器的推荐方法是在on中调用addListenerinitComponent。这将允许扩展类或实例以添加侦听器而不会覆盖它们。例如,new Dummy.RevEvents({listeners: destroy ..})会覆盖您示例中可能不需要的afterrender侦听器。

相关问题