Difference Between this.getView().byId(), this.byId(), and sap.ui.getCore().byId()

那年仲夏 提交于 2019-12-18 07:13:21

问题


Can I know the difference and performance when I use

var myControl = this.getView().byId("myIDhere");
var myControl = this.byId("myIDhere");
var myControl = sap.ui.getCore().byId("myIDhere");

Which of three is best to use to perform operations on control when I use XML views in UI5 apps?


回答1:


  • this.getView().byId(...) is equivalent to this.byId(...). Take a look at the source code to see what byId does:

    // sap.ui.core.mvc.Controller
    Controller.prototype.byId = function(sId) {
      return this.oView ? this.oView.byId(sId) : undefined;
    };
    

    As you can see, this.byId is just a shortcut for this.getView().byId. They both can be used to access controls defined in the view. E.g.:

    this.byId("myPanel");
    
  • sap.ui.getCore().byId(...)API, on the other hand, awaits a fully concatenated global ID which is why you cannot simply exchange this.byId with sap.ui.getCore().byId if the target control is a view descendant.

    sap.ui.getCore().byId("__xmlview0--myPanel"); // <-- Please don't do that!
    

More about IDs:

  • Stable IDs: All You Need to Know
  • For the sake of completeness: How to Access Elements from XML Fragment by ID


来源:https://stackoverflow.com/questions/48639302/difference-between-this-getview-byid-this-byid-and-sap-ui-getcore-byid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!