使用 new Function() 和 string 创建 SAPUI5 控件 [英] Create SAPUI5 control using new Function() and string

查看:50
本文介绍了使用 new Function() 和 string 创建 SAPUI5 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是这个的延续.

我有一个字符串格式的 SAPUI5 控件列表并提取我使用的所需控件:

I have a list of SAPUI5 controls in string format and to extract the required control I use:

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

我在控制台中得到的 oField 对象如下所示:

The oField object I get looks like follows in the console:

M.c…s.f {bAllowTextSelection: true, mEventRegistry: Object, sId: "id",mProperties: d, mAggregations: Object...}

M.c…s.f {bAllowTextSelection: true, mEventRegistry: Object, sId: "id", mProperties: d, mAggregations: Object…}

问题是我无法使用 sap.ui.getCore().byId() 函数获取创建的对象.

The problem is that I cannot get the created object using sap.ui.getCore().byId() function.

我寻找我创建的对象与正常"创建的对象之间的差异

I looked for the differences between the object I create and the object which is created "normally"

var oField = new sap.m.Input();

看起来像这样:

d {bAllowTextSelection: true, mEventRegistry: Object, sId: "id",mProperties: d, mAggregations: Object...}

d {bAllowTextSelection: true, mEventRegistry: Object, sId: "id", mProperties: d, mAggregations: Object…}

显然,这两个对象是不同的.

Obviously, these two object are different.

问题是如何使用 new Function() 创建第二种格式的控件.

The question is how do I create the control of the second format using new Function().

谢谢.

推荐答案

好的,我找到了解决方案.我声明该控件是错误的 - 每个控件的 id 在创建此控件时由用户提供,而当用户没有为控件提供任何 id 时,它会自动提供.

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.

所以,当我这样做

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

我的控件 oField 是使用自动生成和分配的 ID 创建的.

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

然后当我给这个控件提供 id 时 -

And when I give id to this control afterwards -

oField.sId = "someID";

sap.ui.getCore().byId("someID"); 看不到这个控件(老实说我不知道​​为什么.它总是返回 undefined).

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

为了解决这个问题,需要在控件的创建过程中分配控件的id:

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'});

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

HERE 是上述两个声明(有效和无效声明)的 JSBIN 示例.

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

这篇关于使用 new Function() 和 string 创建 SAPUI5 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆