2016-11-03 39 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