javascript - 在初始化函数中定义对象的项

标签 javascript sencha-touch extjs sencha-touch-2

我正在尝试使用函数在组件初始化时在组件内创建项目。

考虑以下因素:

Ext.define('mobi.form.Login',{
    extend:'Ext.form.Panel',
    config:{    
        items: [{
        xtype: 'textfield',
        name: 'Name',
        label: 'Name'
        }]
});
Ext.application({
    viewport: {
        layout:'fit'        
    },
    launch: function(){
        Ext.Viewport.add(Ext.create('mobi.form.Login'));
    }
})

我正在尝试让 mobi.form.login 从在 initialize 上运行的函数生成其配置(或者我可以用来覆盖配置的任何内容)我指定)。

我知道Sencha touch 2有构造函数initialize函数,但它们似乎都有arguments=[](例如一个空数组)

如果我在 ExtJS 4.x 中这样做,这或多或少是这样的:

Ext.define('mobi.form.Login',{
    extend:'Ext.form.Panel',
    initComponent:function(config){
        config=Ext.apply({}.config,{});//make sure config exists    
        config.items= [{
            xtype: 'textfield',
            name: 'Name',
            label: 'Name'
        }]
    Ext.apply(this, config);
    this.callParent(arguments);
    }
});

最佳答案

如果您想这样做,可以使用构造函数初始化

您将用于同步逻辑的构造函数,该逻辑将很快并且您希望在组件初始化之前发生。您可以通过构造函数的第一个参数访问配置:

Ext.define('MyComponent', {
    extend: 'Ext.Component',

    constructor: function(config) {
        console.log(config);

        this.callParent([config]);
    }
});

Ext.application({
    launch: function(){
        Ext.create('MyComponent', { test: 1 })

        // Will log out:
        // {
        //     test: 1
        // }
    }
});

请记住,您总是需要在构造函数中使用config/argumentscallParent

在任何其他情况下,您应该使用initialize,它在所有配置都已初始化之后调用。 :) 我们在内部经常使用它来添加监听器。

initialize: function() {
    this.on({
        ...
    });
}

关于javascript - 在初始化函数中定义对象的项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701370/

相关文章:

android - 拒绝连接到 Cordova android 应用程序中的任何 URL

javascript - extjs 5,在gridpanel上deine dropzone,但不能drop,为什么?

javascript - Bootstrap 附加自定义 JS 和 CSS 重叠 HTML 元素/定位不正确

javascript - asp.net View 中的模态框无法正常工作

android - Sencha 2.4 + cordova 应用程序无法在 Android 上加载 "TypeError: Result of expression ' b.onTargetTouchMove.bind' [undefined] 不是函数。在...”

extjs - 如何将Ext.field.TextArea的大小调整为恰好没有滚动条的文本的大小?

extjs - 使用 Sencha Touch Windows Phone 应用程序 + Windows Azure 移动服务 + 自定义后端推送通知

javascript - HTML 如何填充内容

javascript - 使用 Backbone 和 Node 将模型保存到服务器

javascript - Sencha 触摸窗口调整大小