From the source code below, it is clear that when we create a new button control instance, we can pass into the constructor with an object as argument. If there is a "id" field in the object, UI5 Framework will use it as the button control id. If not, framework will automatically generated an internal id for it as illustrated in previous chapter.
So we can just make a verification accordingly. This time I explicitly assign one ID for my button control:
var oButton1 = new sap.ui.commons.Button({
id: "JerryButton",
text : "Button",
tooltip : "This is a test tooltip",
press : function() {
console.log('Alert from ' + oButton1.getText());
}
});
And it works as expected.
It is a common requirement to obtain the control instance by its id via JavaScript. For practice, add the following two codes after the button is created:
var oSameButton = sap.ui.getCore().byId("JerryButton");
alert(oButton1 === oSameButton);
"True" will be alerted. So how does byId function work?
Wow, there is no magic there. When debugging byId() implementation, I just find out that there is again a central Array maintained by UI5 framework to store all control instances. For my simple application, there is only one button instance there.
Next question, when is a new generated control instance inserted into this central control repository?
You can of course still find out the answer by debugging. However I have another far more efficient way to find out the location of source code without debugging.
Let's consider what will happen if another button is tried to create with the duplicate id? Since it is not acceptable by UI5 framework to have two controls owning exactly the same id. Let's try anyway...
var oButton1 = new sap.ui.commons.Button({
id: "JerryButton",
text : "Button",
tooltip : "This is a test tooltip",
press : function() {
console.log('Alert from ' + oButton1.getText());
}
});
var oButton2s = new sap.ui.commons.Button({
id: "JerryButton" });
Then I find the expected error message in Chrome, and the callstack just guides me to the very code where the central repository is inserted. Just click the hyperlink below:
And then I am navigated to source code line 40705 where the error message is raised. The line 40711 next to it, is just the code which I am searching for the central control repository insertion.
1. The blog How to monitor the control registration and deregistration introduces some advanced tips which makes use of the central repository this.mElements introduced above, to enable you to monitor the life cycle of a given control. Without the tip introduced there, suppose you are debugging a productive UI5 application with hundreds of controls and you just need to monitor the insertion of one control, where do you set breakpoint? You can get answer from that blog.
2. See more information about the topic Support for Unique IDs from SAP help portal.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
26 | |
24 | |
21 | |
13 | |
11 | |
9 | |
9 | |
8 | |
8 | |
8 |