2011-08-23 129 views
0

向按鈕添加功能的最佳方法是什麼? (specfically作用域)ExtJS按鈕處理

骯髒的例子:

form.Login= function(config){ 
if (typeof config !== 'object') { config = {}; } 



    var tbarBottom = { 
    xtype: 'toolbar', 
    dock: 'bottom', 
    items: [{ 
     xtype:'spacer' 
    },{ 
     text: 'Reset', 
     handler:function(button,event){ 
      console.log(this); // ehh... how do I this.reset() from here? 
           //("this" is the buttons scope, not the forms scope) 

     }] 
    } 
    config.dockedItems= [tbarBottom]; 
    form.Login.superclass.constructor.call(this,config); 

Ext.extend(form.Login, Ext.form.FormPanel,{ 

reset: function() { 
    this.reset()// I want to call this function 
} 

}); 

我使用

this.up('form').reset(); 

看到ExtJS的4例子,我還可以

this.ownerCt.ownerCt.reset() 

但是這兩個感覺很莫名其妙地尷尬。 (該.UP要少得多,但因爲我帶觸摸玩,我不認爲「上升」是一個選項)

回答

2

使用此

{ 
    text: 'Reset', 
    handler:function(button,event){ 
    console.log(this); // ehh... how do I this.reset() from here? 
           //("this" is the buttons scope, not the forms scope) 
    }, 
    scope : this 
} 
+0

出於好奇,這事可以在完成所有的JavaScript,還是它的東西ext實現? – Alex

+2

作用域可以像'yourFuction.call(yourScope,function-Arguments)'一樣傳遞給任何函數。像我在上面的代碼中那樣定義範圍是Ext Js傳遞範圍對象的方式 –

+0

太棒了!謝謝 – Alex