在不同控制器中创建片段时出现重复 ID 错误 [英] Duplicate ID error when creating fragments in different Controllers
问题描述
所以...我正在构建一个基本上是 CRUD 的应用程序.在这个应用程序中,我有以下视图/控制器:VisitEdit 和 RequestNew.
在 RequestNew 控制器中,我有一个处理按钮按下的函数:
onRequestNewAddCustomerPress: function(oEvent) {如果(!this.oAddCustomerDialog){this.oAddCustomerDialog = sap.ui.xmlfragment("com.sap.lccapp.fragment.AddCustomer", this);}this.oAddCustomerDialog.openBy(oEvent.getSource());},
我在同一个控制器上有 onExit
函数.它现在是空的,因为我已经用这个对象的 .destroy()
函数(oAddCustomerDialog
)做了很多测试,它继续弹出错误.><小时>
问题是;在 VisitEdit 控制器上,当我第二次尝试使用同一个对话框时,使用与上面相同的代码,它显示以下错误:
<块引用>添加具有重复 ID 'addCustomerNameField' 的元素
ID "addCustomerNameField"
来自我片段中的第一个元素.
虽然我在两种方法上都有if 验证"并且因为它在不同的控制器中,但最后一个被验证的if"对象 (this.oAddCustomerDialog
) 未定义(但它应该没有未定义的值)并且它正在再次创建 sap.ui.xmlfragment
.
片段定义:http://dontpad.com/stackoverflowquestionsapui5
您可以在实例化片段时关联唯一 ID.这样,这个唯一 ID 将带有片段包含的控件 ID 的前缀.
因此,两个不同的代码将是:
onRequestNewAddCustomerPress: function(oEvent) {如果(!this.oAddCustomerDialog){this.oAddCustomerDialog = sap.ui.xmlfragment("idOnNewRequest","com.sap.lccapp.fragment.AddCustomer", this);}this.oAddCustomerDialog.openBy(oEvent.getSource());},
然后:
onVisitEditAddCustomerPress: function(oEvent) {如果(!this.oAddCustomerDialog){this.oAddCustomerDialog = sap.ui.xmlfragment("idOnEdit","com.sap.lccapp.fragment.AddCustomer", this);}this.oAddCustomerDialog.openBy(oEvent.getSource());},
此外,请检查以下文档主题:声明性 XML 或 HTML 片段中的 ID
如果从两个不同的视图调用这些片段,最好使用视图的 ID.我将修改代码以实例化片段,如下所示:
this.oAddCustomerDialog = sap.ui.xmlfragment(this.getView().getId(), "com.sap.lccapp.fragment.AddCustomer", this);
<小时>
从 UI5 1.58 开始,不推荐使用工厂函数 sap.ui.*fragment
.请使用Fragment.load
代替!
Fragment.load({id: this.getView().getId(),name: "com.sap.lccapp.fragment.AddCustomer",控制器:这个,});//返回一个承诺
So... I'm building an application that is basically a CRUD. On this application, I have the following views / controllers: VisitEdit and RequestNew.
At the RequestNew controller, I have a function that handles the press of a button:
onRequestNewAddCustomerPress: function(oEvent) {
if( !this.oAddCustomerDialog ){
this.oAddCustomerDialog = sap.ui.xmlfragment("com.sap.lccapp.fragment.AddCustomer", this);
}
this.oAddCustomerDialog.openBy(oEvent.getSource());
},
And I have at this same Controller the onExit
function. It is now empty, because I have made a LOT of test with the .destroy()
function of this object (oAddCustomerDialog
) and it continues popping up the error.
The problem is; on the VisitEdit controller, when I try to use the same dialog for the second time, with the same code as above, it shows the following error:
Adding element with duplicate id 'addCustomerNameField'
The ID "addCustomerNameField"
is from my first element inside my fragment.
Although I have the 'if verification' on both methods and because it is in different controllers, the last 'if' that is being verified has the object (this.oAddCustomerDialog
) undefined (BUT IT SHOULD NOT HAS UNDEFINED VALUE) and it is creating again the sap.ui.xmlfragment
.
Fragment definition: http://dontpad.com/stackoverflowquestionsapui5
You can associate a unique ID when instantiating fragments. This way this unique ID will be prefix with the IDs of the control the fragment contains.
So, two different codes will be:
onRequestNewAddCustomerPress: function(oEvent) {
if (!this.oAddCustomerDialog) {
this.oAddCustomerDialog = sap.ui.xmlfragment("idOnNewRequest","com.sap.lccapp.fragment.AddCustomer", this);
}
this.oAddCustomerDialog.openBy(oEvent.getSource());
},
and then:
onVisitEditAddCustomerPress: function(oEvent) {
if (!this.oAddCustomerDialog) {
this.oAddCustomerDialog = sap.ui.xmlfragment("idOnEdit","com.sap.lccapp.fragment.AddCustomer", this);
}
this.oAddCustomerDialog.openBy(oEvent.getSource());
},
Also, do check the following documentation topic: IDs in Declarative XML or HTML Fragments
EDIT: If these fragments are being called from two different views, best to use the ID of the view. I would modify the code to instantiate fragment as below:
this.oAddCustomerDialog = sap.ui.xmlfragment(this.getView().getId(), "com.sap.lccapp.fragment.AddCustomer", this);
As of UI5 1.58, the factory function sap.ui.*fragment
is deprecated. Please use Fragment.load
instead!
Fragment.load({
id: this.getView().getId(),
name: "com.sap.lccapp.fragment.AddCustomer",
controller: this,
}); // returns a promise
这篇关于在不同控制器中创建片段时出现重复 ID 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!