为什么本地唯一ID与HCP中的ID不一致?

时间:2016-11-03 08:11:30

标签: sapui5 sap-fiori

我检查了在HCP中运行本地的应用中的元素,ID为application-MaintainMasterData-display-component---addRoute--form,但是当我部署到云时,ID已更改为application-MaintainFleet-Display-component---addRoute--form

应用名称已更改,上层方式为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 个答案:

答案 0 :(得分:1)

动态生成ID。所以你不能依赖它们。这就是为什么你不应该使用sap.ui.getCore().byId()的原因。即使分隔符-----可能会在将来发生变化。

您应始终使用最近视图或组件的byId()方法来解析本地ID。您可以将呼叫链接起来:component.byId("myView").byId("myControl")

在你的eventhandler this中应该引用控制器。对于XMLViews,这应该是没有进一步做的情况。

所以我猜你在使用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.
}
相关问题