question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root View and Controller is instantiated 2 times

See original GitHub issue

OpenUI5 version: 1.44.15

Browser/version (+device/version): Google Chrome Version Version 61.0.3163.100 (Official Build) (64-bit) FAIL

Any other tested browsers/devices(OK/FAIL): Google Chrome Version 64.0.3249.0 (Official Build) canary (64-bit) FAIL

URL (minimal example if possible): com.gk-software.demo.nav-one-component.zip

User/password (if required and possible - do not post any confidential information here):

Steps to reproduce the problem: Please debug the example. Place breakpoint in ProcessBaseController, onInit() method. Start the example, onInit() is called 2 times before showing the view. In the DOM structure App is also present 2 times, specifically included in itself:

screenshot_duplicated_elements

What is the expected result? In manifest.xml should be possible to define rootView and use it as a target. For example:

"sap.ui5": {
		"_version": "1.1.0",
		"rootView": "RootView",
...
"routes": [
			{
				"pattern": "component",
				"name": "componentName",
				"target": "component"
			},
			{
				"pattern": "component/docs",
				"name": "componentDocs",
				"target": "componentDocs"
			}],
			
			"targets": {
				"component": {
					"viewName": "RootView",
					"viewLevel" : 1
				},
				"componentDocs": {
					"parent": "component",
					"viewName": "Docs",
					"transition": "show",
					"viewLevel" : 2
				}
			}

What happens instead? When we use rootView as a target in manifest.xml, rootView and its controller are instantiated 2 times, there are 2 instances in the memory as well as in the DOM structure. Because of that we do not have possibility where to do things that should run only once during the lifecycle, i.e. in the onInit() method.

Any other information? (attach screenshot if possible) The reason why it is happening is that in Component.js RootView is instantiated during component UIComponent.prototype.init.apply(this, arguments); and then the Router matches the target with RootView and thus the second instance of RootView is created.

init: function () {
			  // call the init function of the parent
			  UIComponent.prototype.init.apply(this, arguments);

			  // create the views based on the url/hash
			  this.getRouter().initialize();
		        }

Is it possible to correct this behaviour and enable the possibility to use root view as a target?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
stopcodercommented, Apr 13, 2022

Hi @tanzhaus,

I put the answer first: “No, it’s not possible” and try to explain why it’s not possible.

Both “rootView” and “router” belong directly to the Component. “Router” and “rootView” don’t know about each other. The component builds up a connection between them by telling the router to use “rootView.byId(CONTROL_ID)” (CONTROL_ID is the routing configuration “controlId”) to find the container where the router places the target(s). Therefore the creation and instance management of the “rootView” isn’t in the scope of the router. When the rootView is configured as a routing target, another instance is created because router doesn’t know the existence of the rootView in the component and shouldn’t know about it.

However, there’s solution for the original problem. It can be solved by having a very simple rootView where only the container control is created. The View that should be shown with the empty hash “” should be declared as a target which is then displayed by the route with empty hash.

Best regards, Jiawei

0reactions
tanzhauscommented, Apr 13, 2022

Thank’s a lot Jiawei @stopcoder. Yes, creating a simple “root view” container works fine but some customer are not very happy about that due to increasing number of files and reorganizing existing hierarchies. Thanks to you I can now justify that 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

UI5 Root view and Controller instantiated twice
I believe because of this, my Detail.controller method onInit and onAfterRendering are being called twice. Any solution to this?
Read more >
Root View Controller is getting created twice - Stack Overflow
It turns out that my root view controller is getting created twice in iOS4.3. In MainWindow_iPhone.xib I create my root view controller and ......
Read more >
Displaying and managing views with a view controller
UIViewController contains a content view, accessible from the view property, which serves as the root view of its view hierarchy. To that root...
Read more >
UIViewController's view loading process demystified
She inspired me and I started digging and debugging a bit. I've noticed there are two flows - one when view controller is...
Read more >
1. UI Controllers - Native Mobile Development [Book] - O'Reilly
They are most easily created within the storyboard editor in Xcode. To create a new segue, you must first have two view controller...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found