Javascript extjs3.4.0,为什么可以';我是否配置包含项目的外部容器?
我在配置Ext.Container及其子类时遇到了问题。我一直在尝试这样的东西:Javascript extjs3.4.0,为什么可以';我是否配置包含项目的外部容器?,javascript,extjs,extjs3,Javascript,Extjs,Extjs3,我在配置Ext.Container及其子类时遇到了问题。我一直在尝试这样的东西: var PanelSubclass = Ext.extend(Ext.Panel, { debugID: "DavidPanel", items: [{ debugID: "DavidNumberField", xtype: "numberfield" }] }); new PanelSubclass() //blows up 到目前为止,我已经了解到,在
var PanelSubclass = Ext.extend(Ext.Panel, {
debugID: "DavidPanel",
items: [{
debugID: "DavidNumberField",
xtype: "numberfield"
}]
});
new PanelSubclass() //blows up
到目前为止,我已经了解到,在初始化组件时,Ext.Container将为每个配置的项本身调用add(item)。以下是Ext.Container.add的源代码,以供参考:
add : function(comp){
this.initItems();
var args = arguments.length > 1;
if(args || Ext.isArray(comp)){
var result = [];
Ext.each(args ? arguments : comp, function(c){
result.push(this.add(c));
}, this);
return result;
}
var c = this.lookupComponent(this.applyDefaults(comp));
var index = this.items.length;
if(this.fireEvent('beforeadd', this, c, index) !== false && this.onBeforeAdd(c) !== false){
this.items.add(c);
// *onAdded
c.onAdded(this, index);
this.onAdd(c);
this.fireEvent('add', this, c, index);
}
return c;
}
此方法的第一行调用initItems(),它设置容器的内部项Ext.util.MixedCollection,如下所示:
initItems : function(){
if(!this.items){
this.items = new Ext.util.MixedCollection(false, this.getComponentId);
this.getLayout(); // initialize the layout
}
}
因此,导致我的问题的原因似乎是我在配置中指定了一个项,因此if(!this.items)
检查返回false,并且从未为我的容器设置真正的Ext.util.mixed项集合
但是这没有任何意义,因为你应该能够给一个容器一些物品,所以我想知道,到底是如何创建一个包含任何东西的容器的。我做错了什么 我觉得你真的太过分了。你为什么一直往下看来分析源代码。照医生说的做。我从未使用过ExtJS3.4,我只使用了4.2。这就是为什么我不能说得很具体。在ExtJs 4.2中,您可以编写:
Ext.define('Myapp.MyPanel', {
extend: 'Ext.panel.Panel',
debugID: "DavidPanel",
items: [{
debugID: "DavidNumberField",
xtype: "numberfield"
}]
});
Ext.create('Myapp.MyPanel');
我在Sencha论坛上得到的答案是:
Ext.define('Foo', {
extend: 'Ext.Container',
initComponent: function() {
this.items = [];
Foo.superclass.initComponent.call(this);
}
});
我使用Ext.extend将其转换为此解决方案:
var PanelSubclass = Ext.extend(Ext.Panel, {
debugID: "DavidPanel",
initComponent: function(){
this.items = [{
debugID: "DavidNumberField",
xtype: "numberfield"
}];
PanelSubclass.superclass.initComponent.call(this);
}
});
new PanelSubclass();
这两种方法都会奏效 谢谢你的回答。在extjs3.4的文档中,它说items是一个有效的配置属性,可以提供给容器。在大多数情况下,当事情不顺利,文档没有帮助时,我发现自己在深入研究文档。我不知道4中的情况是否有所改善,但3的文档往往有点糟糕。当然,项目配置是有效的。一定有效。这不是我的意思。我觉得奇怪的是,您扩展了一个内置类,而不是定义自己的类,并且使用new来实例化它。至少在extjs 4.2中,您不能使用new,而是使用Ext.create