2016-05-11 26 views
0

我得到了一個菜單項是這樣的:如何訪問EventListener之外的方法? (SAPUI5)

new sap.ui.unified.MenuItem({ 
    text: "ID", 
    submenu: new sap.ui.unified.Menu({ 
     items: [this.oIdMenuButton = new sap.ui.unified.MenuItem({ 
      text: "IDs anzeigen/ausblenden", 
      icon: "resources/images/check.png", 
      select: this._onShowHideIdRequest 
     })] 
    }) 
}) 

而且像這樣的事件監聽:

_onShowHideIdRequest: function (oControlEvent) { 

} 

此代碼是一個組件內。現在發生的問題是:我無法以自己的身份訪問組件。因爲當我打電話給this.時,我訪問了觸發事件的MenuItem。我如何訪問此EventListener方法之外的方法?我知道有sap.ui.getCore().byId(id)但通常我不知道我的組件的ID。而且我也無法存儲id,因爲我無法訪問EventHandler中的id。

回答

1

用這行代碼修改調用_onShowHideIdRequest,它將訪問組件。

this._onShowHideIdRequest.bind(this); 
+0

我不不知道,你這是什麼意思。當我在活動中'記錄'這個對象時,就和之前一樣,MenuItem被記錄下來... – Chris

+0

對不起,我誤解了你的問題讓我修改我的答案 – Dopedev

+0

非常感謝!這工作正常:) – Chris

0

你可以傳遞參數,以廣達select[1]

// a function that will be called 
function 
// same as above 
[ function ] 
// the function will be called, value of this is the context-object 
[ function, context ] 
// the function will be called, the value of this is the context-object, the data will be passed to the function as argument 
[ data, function, object ] 

例如:

var ctx = {foo: "bar"}; 
new MenuItem({ 
    select: [ 
     function() { 
      console.log(this.foo); 
     }, ctx 
    ] 
}); 

應導致登錄 「欄」