How to reuse SAPUI5 views?

非 Y 不嫁゛ 提交于 2019-12-03 21:31:37

This problem is probably related to the several varaints of using sap.ui.jsview:

  • View definition
  • View instantiation

Calling var pageHeader = sap.ui.jsview("appHeader","view.appHeader") asks the framework to look for a view with the name "view.appHeader" in your project. If there is a view with this name it will create a new instance of it and assigns it the ID "appHeader". If you use the same statement in several modules of your application you ask the framework to create an instance of the view with the same ID everytime. This will probably result in the following Error: adding element with duplicate id 'appHeader'. So when reusing a view avoid using the ID parameter in your instantiation.

One more thing you should consider when reusing a view: Since a view has a function called getControllerName which returns the name of the controller used for this view the framework will create a new instance of this controller for each view you instantiate. This is no bad behaviour but should be known to avoid problems.

At a glance:

View Definition:

sap.ui.jsview(sId, vView) // (e.g. in pageHeader view file)

View Instantiation:

sap.ui.jsview(sName) // (e.g. calling/reusing it in several files) 

In your case:

var pageHeader = sap.ui.jsview("view.appHeader") should do it.

Edit/Appendix:

Since version 1.15 of UI5 you can also use fragments as a lightweight alternative for reusing views. The documentation has some nice examples and explains the advantages of fragments and how they differ from normal views.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!