Inheritance ExtJS4:执行继承的正确方法是什么
我的代码:Inheritance ExtJS4:执行继承的正确方法是什么,inheritance,extjs4,Inheritance,Extjs4,我的代码: Ext.onReady(function() { // Every property is declared inside the class Ext.define('MyCustomPanel1', { extend: 'Ext.panel.Panel', alias: 'mycustompanel1', title: 'I am a custom Panel 1', html: 'This is the content of my custom p
Ext.onReady(function() { // Every property is declared inside the class
Ext.define('MyCustomPanel1', {
extend: 'Ext.panel.Panel',
alias: 'mycustompanel1',
title: 'I am a custom Panel 1',
html: 'This is the content of my custom panel 1',
renderTo: Ext.getBody()
});
Ext.define('MyCustomPanel2', { // HTML is declared inside the class, title inside the config, constructor overridden
extend: 'Ext.panel.Panel',
alias: 'mycustompanel2',
renderTo: Ext.getBody(),
html: 'This is the content of my custom panel 2',
config: {
title: 'I am a custom Panel 2'
},
constructor: function(config) {
this.callParent(arguments);
this.initConfig(config)
}
});
Ext.define('MyCustomPanel3', { // Title and HTML declared inside config, constructor overridden
extend: 'Ext.panel.Panel',
alias: 'mycustompanel3',
renderTo: Ext.getBody(),
config: {
title: 'I am a custom Panel 3',
html: 'This is the content of my custom panel 3'
},
constructor: function(config) {
this.callParent(arguments);
this.initConfig(config)
}
});
Ext.define('MyCustomPanel4', { // title and html inside of initComponent, title override in instance declaration doesn't hold. HTML property is empty on render
extend: 'Ext.panel.Panel',
alias: 'mycustompanel4',
renderTo: Ext.getBody(),
initComponent: function(config) {
Ext.apply(this, {
title: 'I am a custom Panel 4',
html: 'This is the content of my custom panel 4'
})
this.callParent(arguments);
}
});
Ext.define('MyCustomPanel5', { // title declared inside config, html set inside of initComponent. Both initComponent and constructor are used.
extend: 'Ext.panel.Panel',
alias: 'mycustompanel5',
renderTo: Ext.getBody(),
config: {
title: 'I am a custom Panel 5'
},
constructor: function(config) {
this.callParent(arguments);
this.initConfig(config);
},
initComponent: function(config) {
Ext.apply(this, {
html: 'This is the content of my custom panel 5'
})
this.callParent(arguments);
}
});
Ext.create('MyCustomPanel1', {
title: 'I am custom Panel 1 - Instance!',
html: 'This is the content of my custom panel 1 - Instance!'
})
Ext.create('MyCustomPanel2', {
title: 'I am custom Panel 2 - Instance!',
html: 'This is the content of my custom panel 2 - Instance!'
})
Ext.create('MyCustomPanel3', {
title: 'I am custom Panel 3 - Instance!',
html: 'This is the content of my custom panel 3 - Instance!'
})
Ext.create('MyCustomPanel4', {
title: 'I am custom Panel 4 - Instance!',
html: 'This is the content of my custom panel 4 - Instance!'
})
Ext.create('MyCustomPanel5', {
title: 'I am custom Panel 5 - Instance!',
html: 'This is the content of my custom panel 5 - Instance!'
})
})
结果(通过JSFiddle.net):
以上哪种方法是通过扩展现有对象来创建对象的正确方法?每种方法的优缺点是什么?在哪里可以找到关于ExtJS4继承的更多信息(http://docs.sencha.com/ext-js/4-0/#/guide/class_system)?
谢谢,我在Sencha论坛上问了这个问题,以下是我从Saki那里得到的答案: 扩展时使用构造函数还是initComponent取决于 取决于你想做什么。initComponent将从父级运行 构造函数,但稍后,在一些内部变量 已经初始化了,所以在某些情况下,有时您需要这样做 不是 在任何情况下,我都不会在Ext.define中使用renderTo,因为它会导致 实例化后立即呈现的组件,即 并不总是你想要的。此外,initConfig应该位于父级之前 调用构造函数,否则它就没用了 已在父调用中初始化 你可能还想在我的签名中读到“写一个大…”。这 该文档是为Ext的早期版本编写的,所以代码示例是这样做的 不再适用,但原则是一样的
根据我目前在ExtJS4上发现的情况,下面是我扩展现有组件的方法(下面是在textfield上创建的示例组件) 我使用构造函数方法,到目前为止没有发现任何问题:
Ext.define('Ext.pnc.Textfield', {
extend: 'Ext.form.field.Text',
alias: 'widget.pnctextfield',
config:{
focusCls:'focusClassFieldPnC',
fieldCls:'baseClassFieldPnC'
},
constructor:function(cfg){
this.callParent(arguments);
this.initConfig(cfg);
this.on('beforerender',this.beforeRender);
},
beforeRender:function(){
if(!this.allowBlank){
this.labelStyle = 'color:#ff0000';
}
}
});
希望这能有所帮助。但是在
Ext.Define
中使用Config
声明怎么样?这是个好主意还是个坏主意?有什么想法吗?