2

Is there a way (except using eval()) to build a sapui5 control if the constructor is a string?

For example, I want to build a text control:

var oText = new sap.m.Text({});

but the part "new sap.m.Text({})" is actually a string. I tried eval():

var sObj = "new sap.m.Text({});";
var oObj = eval(sObj);
oObj.setText("Hello"); 

and it works.

The question is if there is another, more secure way to do it.

keshet
  • 1,716
  • 5
  • 27
  • 54

1 Answers1

2

Based on @dfsq's comment you can use new Function:

var fControlGenerator = function (sControlCommand) {
    return new Function("return " + sControlCommand)();
};
var oControl = fControlGenerator("new sap.m.Text()");
hirse
  • 2,394
  • 1
  • 22
  • 24
  • You had posted your answer while I was typing mine, which is almost the same as yours. You just saved me some time, good sir! – keshet Aug 24 '15 at 08:02