[英]Best Practices concerning initComponent() in Ext.define()
I'm writing all my components in ExtJS's new MVC fashion using Ext.define()
.我正在使用
Ext.define()
以 ExtJS 的新 MVC 方式编写我的所有组件。
I struggle a bit whether define properties inside of initComponent()
or by simply setting them like property: 42,
.我有点
initComponent()
是在initComponent()
内部定义属性还是简单地将它们设置为property: 42,
.
Are there widely accepted best practices?是否有被广泛接受的最佳实践?
I'm staggering between using initComponent()
only when necessary (ie. when I want something dynamic or set a scope) which keeps the function shorter and spares me some ugly this.
我只是在必要时(即,当我想要一些动态的东西或设置一个范围时
initComponent()
才使用initComponent()
之间摇摆initComponent()
,这使函数更短,并使我免于一些丑陋的this.
s and using it always which has the benefit, that I'd never have to move former properties to initComponent()
just because I want to make it more dynamic. s 并始终使用它有好处,我永远不必将以前的属性移动到
initComponent()
只是因为我想让它更动态。
Unfortunately, Sencha's docs don't tell much about that and the available examples seem to do as they want.不幸的是,Sencha 的文档并没有详细说明这一点,并且可用的示例似乎可以按照他们的意愿行事。
Personal practice, I will declare variables in the properties area when the个人实践,我会在属性区声明变量的时候
x
, y
, width
, height
x
、 y
、 width
、 height
title
, saveBtnTxt
, url
, fields
, iconCls
title
、 saveBtnTxt
、 url
、 fields
、 iconCls
Then I will declare items
, listeners
, this.on
, Ext.apply(me, {..})
or anything that requires the scope of the obj ( this
, me
), to sit inside my initComponent
.然后我将声明
items
、 listeners
、 this.on
、 Ext.apply(me, {..})
或任何需要 obj ( this
、 me
)范围的东西,以位于我的initComponent
。 Or stuff that should be modified/overridden before everything is setting up so user will not break my component by overriding some of the important variables.或者在一切设置之前应该修改/覆盖的东西,这样用户就不会通过覆盖一些重要的变量来破坏我的组件。
Of course that'll serve as my guidance.当然,这将作为我的指导。 2 cents
2 美分
EDIT编辑
About the ugly this
, I have used the variable me
widely in my app, and it looks a lot cleaner than this
.关于丑陋的
this
,我在我的应用程序中广泛使用了变量me
,它看起来比this
干净很多。 It benefits me from changing scopes less frequently too.它也从不那么频繁地更改范围中受益。
I want to add to Lionel's answer that it is better to declare any non-primitive config in initComponent
.我想补充一下 Lionel 的回答,最好在
initComponent
声明任何非原始配置。 (By primitive I mean string, boolean and number). (原语是指字符串、布尔值和数字)。 Array and Object go into
initComponent
. Array 和 Object 进入
initComponent
。
So definition should look like this:所以定义应该是这样的:
Ext.define('My.NewClass', {
extend: 'OldClass',
// here all primitive configs:
cls: 'x-my-cls',
collapsible: true,
region: 'west',
// and so on ...
initComponent: function() {
// here you declare non-primitive configs:
this.tbar = [/* blah-blah */];
this.columns = [/* blah-blah */];
this.viewConfig = {/* blah-blah */};
// and so on ...
this.callParent(arguments);
}
// other stuff
}
The reason why you should put all non-primitive configs into initComponent is that otherwise configs of all instances will refer to the same objects.您应该将所有非原始配置放入 initComponent 的原因是,否则所有实例的配置将引用相同的对象。 For example if you define NewClass like:
例如,如果您像这样定义 NewClass:
Ext.define('My.NewClass', {
extend: 'OldClass',
bbar: Ext.create('Ext.toolbar.Toolbar', {
// ...
bbar
s of all instances will refer to the same object.所有实例的
bbar
将引用同一个对象。 And therefore every time you create new instance bbar disappears from the preveous instance.因此,每次您创建新实例时,bbar 都会从之前的实例中消失。
Ext.define('My.Group', {
// extend: 'Ext.form.FieldSet',
xtype : 'fieldset',
config : {
title : 'Group' + i.toString(),
id : '_group-' + i.toString()
},
constructor : function(config) {
this.initConfig(config);
return this;
},
collapsible: true,
autoScroll:true,
.....
});
you can use it as follow.您可以按如下方式使用它。
handler : function() {
i = i + 1;
var group = new My.Group({
title : 'Group' + i.toString(),
id : '_group-' + i.toString()
});
// console.log(this);
// console.log(group);
Ext.getCmp('panelid').insert(i, group);
Ext.getCmp('panelid').doLayout();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.