2012-05-31 27 views
1

我創建這樣煎茶觸摸2 ..訪問配置值而創建視圖

Ext.define('App.view.Message', { 
    extend: 'Ext.Panel', 
    alias: 'widget.message', 
    config: { 
     layout: 'vbox', 
     bigText: 'big Text', 
     smallText: 'small text', 
     imageUrl: 'imageurl', 
     oT: '', 
     description: 'message description', 

     items: [ 
      { 
      flex: 3, 
      xtype: 'container', 
      layout: 'hbox', 
      items: [ 
       { 
        xtype: 'container', 
        flex: 1, 
        items: [ 
         { 
          html: '<h3>' + this.getBigText() + '</h3>' 
         }, 
         { 
          html: '<h5>' + this.getSmallText() + '</h5>' 
         } 
        ] 
       }, 
       { 
        xtype: 'image', 
        src: this.getImageUrl(), 
        flex: 1 
       } 
      ] 
     }, 
      { 
      flex: 6, 
      xtype: 'container', 
      layout: 'vbox', 
      items: [ 
       { 
        flex:1, 
        html: '<h3>' + this.getBigText() + '</h3>' 
       }, 
       { 
        flex:5, 
        html: '<p>'+this.getDescription()+'</p>' 
       } 
      ] 
     }, 
      { 
      flex: 1, 
      xtype: 'button', 
      text: this.getOT() 
     } 
     ] 
    } 
}) 

我需要訪問時,將創建該視圖(配置值)將被傳遞的值,同時創建視圖的圖。
這樣的語句this.getDescription()this.getBigText()等等等等都產生錯誤.. 我想是THT的觀點應與配置值,我通過當IM創建視圖..
是我應該做的呈現?

回答

1

當您定義並加載「查看」文件時,瀏覽器會遍歷每一行。 「視圖」實例當時未創建。所以,當然這個不能在那裏找到。

但是,您可以使用「this」裏面的初始化函數。所以,你可以寫你這樣的代碼:

Ext.define('App.view.Message', { 
    extend: 'Ext.Panel', 
    xtype: 'message', 
    config: { 
     layout: 'vbox', 
     bigText: 'big Text', 
     smallText: 'small text', 
     imageUrl: 'imageurl', 
     oT: '', 
     description: 'message description' 
    }, 

initialize : function(){ 
    this.add([ 
      { 
      flex: 3, 
      xtype: 'container', 
      layout: 'hbox', 
      items: [ 
       { 
        xtype: 'container', 
        flex: 1, 
        items: [ 
         { 
          html: '<h3>' + this.bigText + '</h3>' 
         }, 
         { 
          html: '<h5>' + this.smallText + '</h5>' 
         } 
        ] 
       }, 
       { 
        xtype: 'image', 
        src: this.imageUrl, 
        flex: 1 
       } 
      ] 
     }, 
      { 
      flex: 6, 
      xtype: 'container', 
      layout: 'vbox', 
      items: [ 
       { 
        flex:1, 
        html: '<h3>' + this.bigText + '</h3>' 
       }, 
       { 
        flex:5, 
        html: '<p>'+ this.description +'</p>' 
       } 
      ] 
     }, 
      { 
      flex: 1, 
      xtype: 'button', 
      text: this.oT 
     } 
     ]); 

     this.callParent(arguments); 
} 
}); 
0

爲此,我建議你添加您項目視圖中的初始化函數,像這樣:

... 
    config:{ 
    ... 
    }, 
    initialize: function(){ 
    var me = this; 
    me.callParent(); 

    var container = Ext.create('Ext.Container',{ 
     title = this.config.bigText 
    }); 

    me.add(container); 
    } 
    ... 

我不知道是否有一個最好的解決方案,但這個工作。

2

Sencha Touch 2不會自動爲您的配置生成getter和setter。不過,也有2種方式,做你需要的東西:

  • 直接通過Ext.getCmp('your_component_id').config.bigText(類似於其他CONFIGS)

  • 手動創建的getter & setter方法爲您定製的配置,例如獲取&設置配置值,你必須定義這樣的事情作爲你的視圖組件的配置:

    getBigText: function(){return this.bigText;}

    setBigText: function(value) {this.bigText = value;}

希望這有助於。

4

看起來你必須重寫構造,並獲得配置值:

Ext.define('App.view.Message', { 
extend: 'Ext.Panel', 
alias: 'widget.message', 

config: { 
    layout: 'vbox', 
    bigText: 'big Text', 
    smallText: 'small text', 
    imageUrl: 'imageurl', 
    oT: '', 
    description: 'message description' 
}, 

constructor: function(config) 
{ 
    config.items = [ 
     { 
      flex: 3, 
      xtype: 'container', 
      layout: 'hbox', 
      items: [ 
       { 
        xtype: 'container', 
        flex: 1, 
        items: [ 
         { 
          html: '<h3>' + config.bigText + '</h3>' 
         }, 
         { 
          html: '<h5>' + config.smallText + '</h5>' 
         } 
        ] 
       }, 
       { 
        xtype: 'image', 
        src: config.imageUrl, 
        flex: 1 
       } 
      ] 
     }, 
     { 
      flex: 6, 
      xtype: 'container', 
      layout: 'vbox', 
      items: [ 
       { 
        flex:1, 
        html: '<h3>' + config.bigText + '</h3>' 
       }, 
       { 
        flex:5, 
        html: '<p>'+config.description+'</p>' 
       } 
      ] 
     }, 
     { 
      flex: 1, 
      xtype: 'button', 
      text: config.oT 
     } 
    ]; 

    this.callParent(arguments); 
} 

});

0

一個簡單的解決方案是在initialize()函數中這樣做,例如。

this.config.title = 'my title'; 
this.config.iconCls = 'info'; 
this.callParent();