Skip to content Skip to sidebar Skip to footer

Create SAPUI5 Control Using New Function() And String

This question is a continuation of this one. I have a list of SAPUI5 controls in string format and to extract the required control I use: var sDefaultControlConstructor = 'new sap.

Solution 1:

OK, I have found the solution. I was declaring the control wrong - the id to every control is given by user when this control is being created, while when user doesn't give any id to the control, it is given automatically.

So, when I did

var sDefaultControlConstructor = "new sap.m.Input()"; 
var sConstructor = "return " + sDefaultControlConstructor;
var oConstructor = new Function(sConstructor);
var oField = oConstructor();

my control oField was created with automatically generated and assigned id.

And when I give id to this control afterwards -

oField.sId = "someID";

this control cannot be seen by sap.ui.getCore().byId("someID"); (I honestly don't know why. It always return undefined).

To fix the issue, its required to assign control's id during the control's creation:

var oConstructor = new Function("a", "return new sap.m.Input(a)");
var oField = oConstructor({id:'someID'});

OR

var sId = 'someID';
var oConstructor = new Function("a", "return new sap.m.Input(a)");
var oField = oConstructor({id:sId});

HERE is JSBIN example of two above declarations (working and not working ones).


Post a Comment for "Create SAPUI5 Control Using New Function() And String"