到initComponent()或不到initComponent() [英] to initComponent() or not to initComponent()
问题描述
例如,在Sencha自己的 MVC应用程序架构文档中,当首次创建网格视图中,他们在initComponent()方法中定义了内联存储。 (见定义视图部分)
进一步下来,当他们把存储分解成一个单独的类时,他们把定义移到了initComponent()之外。有一个有用的评论,提请注意这个事实,但没有解释。 (见创建模型和存储部分)
我猜这个原因应该是显而易见的,但我错过了。任何指针?
如果您对ExtJS类系统的工作原理不了解,您可能需要遵循:
声明
initComponent()
中的所有非原始类型。 / strong>
术语
- 原始类型 - 字符串,布尔,整数等。
- 非原语 - 数组和
说明
如果要创建扩展的组件不止一次,任何声明为配置选项( initComponent
之外)的非原始配置将在所有实例之间共享。
因此,许多人在多个选项卡上创建扩展组件(通常是扩展网格)时遇到问题。
这个行为在sra的回答下面和这个Skirtle的屁股文章。您可能还想阅读此SO问题。
I struggle when building an app in ExtJS 4, and part of that is confusion on when to configure something in initComponent() and when not to...
For example, in Sencha's own MVC Application Architecture doc, when first creating the grid view, they defined the inline store in the initComponent() method. (See "Defining a View" section)
Further down, when they factored out the store into a separate class, they moved the definition outside of initComponent(). There is a helpful comment that draws attention to this fact, but there is no explanation. (See Creating a Model and Store section)
I guess the reason is supposed to be obvious, but I'm missing it. Any pointers?
If you do not have a deep understanding of how ExtJS class system work, you may want to follow this:
Declare all non-primitive types in
initComponent()
.
Terminology
- Primitive types - strings, booleans, integers, etc.
- Non-Primitives - arrays & objects.
Explanation
If the component you extend is to be created more than once, any non-primitive configs declared as a config option (outside initComponent
) will be shared between all instances.
Because of this, many people experienced issues when an extended component (typically an extended grid) is created on more than one tab.
This behaviour is explained in sra's answer below and in this Skirtle's Den article. You may also want to read this SO question.
这篇关于到initComponent()或不到initComponent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!