使用参数初始化

时间:2013-05-27 19:02:29

标签: extjs sencha-touch sencha-touch-2 sencha-architect

这是我的代码,我想知道我的for-loop限制(IE 8)如何在我的构造函数中有一个参数,所以我可以用特定的行数构建这个视图?然后,如何初始化这样的观点?

谢谢!

Ext.define('Prototype.view.FoodPanel', {
extend: 'Ext.Panel',
alias: 'widget.foodpanel',

config: {
    height: 455,
    id: 'FoodPanel',
    layout: {
        type: 'vbox'
    },
    scrollable: 'vertical',
    items: [
        {
            xtype: 'panel',
            height: 47,
            id: 'rowTitle',
            layout: {
                type: 'hbox'
            },
            items: [
                {
                    xtype: 'textfield',
                    id: 'column_title_source',
                    width: 100,
                    label: 'Source',
                    labelWidth: '100%',
                    readOnly: false
                },
                {
                    xtype: 'textfield',
                    id: 'column_title_user',
                    width: 100,
                    label: 'User',
                    labelWidth: '100%',
                    readOnly: true
                },
                {
                    xtype: 'textfield',
                    id: 'column_title_object',
                    width: 160,
                    label: 'Object',
                    labelWidth: '100%',
                    readOnly: true
                },
                {
                    xtype: 'textfield',
                    disabled: false,
                    id: 'column_title_factor',
                    width: 100,
                    label: 'Factor',
                    labelWidth: '100%',
                    readOnly: true
                }
            ]
        }
    ]
},

initialize: function() {
    this.callParent();
    var rows=[];
    for(var i=0; i<8;i++)
    {
        var row = Ext.create(Prototype.view.RowModel);
        if(i===4)
        {
            row.getComponent('mybutton').hide();
        }
        console.log(row.down('#rowLabel')._label);
        rows.push(row);
    }
    this.add(rows);
}

});

1 个答案:

答案 0 :(得分:3)

按照惯例,Ext组件的构造函数只需要一个参数,即config对象。否则就是不好的做法。

但是,您可以在配置中自由添加所需内容...

例如,在配置定义中添加numberOfLines选项。您可以稍后通过this.numberOfLines或生成的getter this.getNumberOfLines()访问该选项的值:

Ext.define('Prototype.view.FoodPanel', {
    // ...

    ,config: {
        // ...
        /**
         * Here's how you document a config option.
         * 
         * @cfg {Integer} [numberOfLines=8]
         */
        ,numberOfLines: 8 // default value of 8
    }

    ,initialize: function() {
        this.callParent();

        var n = this.getNumberOfLines();

        for (var i=0; i<n; i++) {
            // ...
        }

        // ...
    }
});

然后你可以像往常一样在配置中传递选项:

Ext.create('Prototype.view.FoodPanel', {
    numberOfLines: 20
    // any other option you like...
});
相关问题