首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >initComponent函数在extjs4.1中的用途是什么?

initComponent函数在extjs4.1中的用途是什么?
EN

Stack Overflow用户
提问于 2013-06-05 14:39:05
回答 1查看 11.9K关注 0票数 12

谁能告诉我extjs4.1中initComponent函数的用法是什么?请举个例子

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-05 17:06:19

此方法类似于组件的constructor。它由真正的constructor调用,是一个很好的钩子点,用来定制组件的初始化(正如名称中所说的!)。

除非在极少数情况下,否则应该覆盖initComponent而不是constructor,因为已经进行了更基本的初始化。最值得注意的是,传递给构造函数的配置对象已经合并到对象中。

假设你想要定制一个组件的配置,比如设置它的width。如果您尝试在构造函数中执行此操作,则必须首先检查是否向我们传递了一个配置对象(以避免尝试在undefined上设置属性),并且您将会重写配置对象,这是一种糟糕的做法。如果您在this中设置该选项,则该选项可能会被配置对象覆盖。如果更改配置对象中的值,则会修改该对象,从而打破调用代码的预期(即重用配置对象将产生意外的结果)。在initComponent中,值始终为this.width,您不必担心配置。

另一个有趣的地方是,initComponent是创建子组件(用于容器)、存储、视图、模板等的地方。因此,在调用超类initComponent方法之前,您可以确保它们尚未被使用或需要(例如,添加项目、创建存储等)。另一方面,一旦调用了超级方法,就可以保证所有这些依赖项都已创建和实例化。因此,例如,这是向依赖项添加侦听器的好地方。

话虽如此,但请记住,initComponent中不会发生渲染。创建并配置子组件,但尚未创建其DOM元素。要影响渲染,您必须使用与渲染相关的事件,或者查找afterRenderonRender方法...

以下是带插图的摘要:

代码语言:javascript
复制
constructor: function(config) {

    // --- Accessing a config option is very complicated ---

    // unsafe: this may be changed by the passed config
    if (this.enableSomeFeature) { ... }

    // instead, you would have to do:
    var featureEnabled;
    if (config) { // not sure we've been passed a config object
        if (Ext.isDefined(config.featureEnabled)) {
            featureEnabled = config.featureEnabled;
        } else {
            featureEnabled = this.enableSomeFeature;
        }
    } else {
        featureEnabled = this.enableSomeFeature;
    }
    // now we know, but that wasn't smooth
    if (featureEnabled) {
        ...
    }


    // --- Even worse: trying to change the value of the option ---

    // unsafe: we may not have a config object
    config.enableSomeFeature = false;

    // unsafe: we are modifying the original config object
    (config = config || {}).enableSomeFeature = false;

    // cloning the config object is safe, but that's ineficient
    // and inelegant
    config = Ext.apply({enableSomeFeature: false}, config);


    // --- Super method ---

    this.callParent(arguments); // don't forget the arguments here!

    // --------------------

    // here initComponent will have been called
}

,initComponent: function() {

    // --- Accessing config options is easy ---

    // reading
    if (this.enableSomeFeature) { ... }

    // or writing: we now we change it in the right place, and
    // we know it has not been used yet
    this.deferRender = true;


    // --- Completing or changing dependant objects is safe ---
    // items, stores, templates, etc.

    // Safe:
    // 1. you can be sure that the store has not already been used
    // 2. you can be sure that the config object will be instantiated
    //    in the super method
    this.store = {
        type: 'json'
        ...
    };


    // --- However that's too early to use dependant objects ---

    // Unsafe: you've no certitude that the template object has
    // already been created
    this.tpl.compile();


    // --- Super method ---

    this.callParent();

    // --------------------


    // Safe: the store has been instantiated here
    this.getStore().on({
        ...
    });


    // will crash, the element has not been created yet
    this.el.getWidth();
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16933161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档