Ext.define() 中关于 initComponent() 的最佳实践 [英] Best Practices concerning initComponent() in Ext.define()
问题描述
我正在使用 Ext.define()
以 ExtJS 的新 MVC 方式编写我的所有组件.
I'm writing all my components in ExtJS's new MVC fashion using Ext.define()
.
我有点纠结是在 initComponent()
内部定义属性还是简单地将它们设置为 property: 42,
.
I struggle a bit whether define properties inside of initComponent()
or by simply setting them like property: 42,
.
是否有被广泛接受的最佳实践?
Are there widely accepted best practices?
我在仅在必要时(即,当我想要动态或设置范围时)使用 initComponent()
之间摇摆不定,这可以使函数更短,并为我省去一些难看的 this.
s 并始终使用它有好处,我永远不必将以前的属性移动到 initComponent()
仅仅因为我想让它更动态.
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.
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.
不幸的是,Sencha 的文档并没有详细说明这一点,而且可用的示例似乎可以满足他们的要求.
Unfortunately, Sencha's docs don't tell much about that and the available examples seem to do as they want.
推荐答案
个人实践,我会在属性区声明变量
Personal practice, I will declare variables in the properties area when the
- 定义大小的变量,如
x
、y
、width
、height
- 等待被覆盖或可定制的变量,例如
title
、saveBtnTxt
、url
、fields
,iconCls
- 一些常量,它们会有特殊的前缀,所以不会那么容易被覆盖
然后我会声明items
、listeners
、this.on
、Ext.apply(me, {..})
或任何需要 obj 范围的东西(this
、me
),放在我的 initComponent
中.或者应该在一切设置之前修改/覆盖的东西,这样用户就不会通过覆盖一些重要的变量来破坏我的组件.
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
. 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.
当然,这将作为我的指导.2 美分
Of course that'll serve as my guidance. 2 cents
编辑
关于丑陋的this
,我在我的应用中广泛使用了变量me
,它看起来比this
要干净很多.这也让我从不那么频繁地更改范围中受益.
About the ugly this
, I have used the variable me
widely in my app, and it looks a lot cleaner than this
. It benefits me from changing scopes less frequently too.
这篇关于Ext.define() 中关于 initComponent() 的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!