2013-05-27 54 views
1

這裏是我的代碼,我想知道如何把我的for循環限制(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); 
} 

}); 

回答

3

按照慣例,Ext組件的構造函數只需要一個參數,即配置對象。否則做不好的做法。

不過,你可以自由地添加你的配置要什麼...

例如,在配置定義添加numberOfLines選項。您可以稍後訪問選項通過this.numberOfLines或價值產生的吸氣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... 
}); 
+0

我會嘗試明天,對我來說似乎合乎邏輯,非常感謝! – Fawar

+0

如何添加東西在sencha架構中沒有配置字段。你會知道嗎? – Fawar

+0

不,對不起,我從來沒有使用架構師......但幾乎肯定有一些方法可以直接編輯您的類代碼。如果在編輯的類的代碼中沒有「config」屬性,則可以將其自己添加到類定義中。只要記住,如果你不是從一個已經擁有它的Ext類中進行擴展,就必須在構造函數中調用'this.initConfig(config)'(參見文檔'Ext.Base.initConfig')。 – rixo