2016-11-03 37 views
0

我在運行中HCP本地應用程序檢查元素,id爲application-MaintainMasterData-display-component---addRoute--form,但是當我部署到雲中,ID改爲application-MaintainFleet-Display-component---addRoute--form爲什麼本地唯一ID與HCP中的ID不一致?

的應用程序的名稱改變,並在上流方式display,這使我的sap.ui.getCore().byId()在雲中失敗。我正在說明爲什麼會發生這種情況。

我讀過參考資料,我在事件處理程序中,我需要oEvent範圍,因此this.getView().byId()this.createId()不適用於我。

編號:

sap.ui.getCore().byId() returns no element

https://sapui5.netweaver.ondemand.com/sdk/#docs/guide/91f28be26f4d1014b6dd926db0e91070.html

========= ========= UPDATE

我也試過sap.ui.getCore().byId("application-MaintainMasterData-display-component---addRoute").byId("form"),但同樣的問題,view id在雲中是application-MaintainFleet-Display-component---addRoute

回答

1

這些ID是動態生成的。所以你不能依靠他們。這就是爲什麼你不應該使用sap.ui.getCore().byId()。即使分離器-----可能在未來發生變化。

您應始終使用最近視圖或組件的byId()方法來解析本地ID。您可以鏈接電話:component.byId("myView").byId("myControl")

在您的事件處理程序this應該引用控制器。對於XMLView,這應該是沒有進一步做的情況。

所以我想你正在使用JSViews?如果在代碼中附加事件處理程序,則可以始終爲attachWhatever()函數提供第二個參數:在事件處理程序中變爲this的對象。

Controller.extend("myView", { 
    onInit:function(){ 
    var button = this.byId("button1"); 
    button.attachPress(this.onButtonPress, this); //The second parameter will become 'this' in the onButtonPress function 
    }, 
    onButtonPress: function(oEvent){ 
    console.log(this); //this is the controller 
    var buttonPressed = oEvent.getSource(); //get the control that triggered the event. 
    var otherControl = this.byId("table"); //access other controls by id 
    var view = this.getView(); //access the view 
    } 
}); 

如果您正在使用settings-object-syntax,則可以爲事件提供一個數組。它應該包含的處理功能,這應該成爲對象this

createContent:function(oController){ 
    return new Button({ 
    text: "Hello World", 
    press: [ 
     function(oEvent){ console.log(this); }, //the event handler 
     oController //oController will be 'this' in the function above 
    ] 
    }); 

如果要連接到非UI5事件,你可以隨時使用閉包來提供視圖或控制器的處理函數:

onInit:function(){ 
    var that = this; //save controller reference in local variable 
    something.on("event", function(){ console.log(that); }); 
       //you can use that local variable inside the eventhandler functions code. 
} 
+0

我想'deleteIcon.attachPress(this.onDeleteStop,這一點);'但是'onDeleteStop:功能(oEvent,oController){返回oController}','oController'是'undefined',我覺得我沒有」噸得到'設置對象語法'的重點? – Tina

+0

它應該是'onDeleteStop:function(oEvent){console。日誌(本); }'。 *這個*是你的控制器或者其他*這個*是你提供給attach ...函數的。 – schnoedel

+0

我已經將attachPress的代碼示例添加到我的答案中。 – schnoedel