修改initComponent()中的项

K..*_*K.. 5 extjs extjs4.1

我在initComponent() 问题中创建了一些项目,this.items以某种方式引用了类变量,而不是实例变量.

因此,当我制作两个实例时,我最终得到了两个按钮.

items: [],
initComponent: function() {
  this.items.push( { xtype: 'button', ... }) ;
  this.callParent( arguments );
}
Run Code Online (Sandbox Code Playgroud)

因为每次推入新元素时我都必须使用push.

是否有一些实例等同于this.items我可以在创建按钮之前修改定义的位置,或者我是否必须手动检查重复项?

Joh*_*est 7

你不应该 return this.callParent( arguments );

这就足够了:

initComponent: function() {
    var me = this;
    me.items = { xtype: 'button', ... }; //Are you sure items is filled up here?
    me.callParent();
}
Run Code Online (Sandbox Code Playgroud)

此外,如果您正在编写自己的"组件"并且想要传递参数,请Ext.create始终执行以下操作:

constructor: function (config) {
    var me = this;
    Ext.apply(me, config); 
    me.callParent();
}
Run Code Online (Sandbox Code Playgroud)

这将使用您提交的项目覆盖您班级中的项目声明 Ext.create()