Skip to Content
0

Create controller function at runtime

Feb 12 at 10:34 AM

28

avatar image
Former Member
sap.ui.controller("my.own.controller", {
		doSomething : function() {
			alert("Hello World!");
		}
	});	
sap.ui.jsview("my.own.view", {
		getControllerName : function() {
			return "my.own.controller";
		},
		createContent : function(oController) {
			 return new sap.ui.commons.Button({
			 text : "doSomething",
			press : oController.doSomething
			});
		}
	});
var myView = sap.ui.view({
		type : sap.ui.core.mvc.ViewType.JS,
		viewName : "my.own.view",
		id : "test1"
	});

Hi Friends,

Above I create a view and the controller at runtime. But now I need to add a function to the controller at runtime. Here I know the function "doSomething" at start. But I have a couple of other functions I don't know from start. How can I add a function at later point dynamically?

Thanks Joerg

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Emanuele Ricci Feb 13 at 10:39 AM
0

Just out of curiosity. Why do you need to handle things this way with JSViews?

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Feb 13 at 12:17 PM
0

Hi Arjun,

the Problem is, the Controller does not exist at start up. It is created at run time. The Controller source is saved in the backend. So at runtime I read the data from the backend and when I create the controller based on the source, just read from the backend.

It means, I have to create the controller based on the source from the backend.

I use JSViews, because I have the real ID of the controls. Not something like --View0--blablabla.

Rg. Jorg

Show 1 Share
10 |10000 characters needed characters left characters exceeded

in most case, it is wrong design....

0
Arjun Biswas Feb 13 at 05:17 AM
0

Hi joerg arndt,

Maybe it is not the right approach, but you could try something like this (I have explained it in a different scenario):

In the view :
<Button text="Press" press="dynFunc"/>

At your controller :

dynFunc:function(){
				dynCreatedFunction();
				function dynCreatedFunction(){                 //Defining your dynamic function.
					alert("Hello");
				}
				this.dynCreatedFunction = dynCreatedFunction; // Adds the function to your controller.
			},

Hope this helps,

Regards,

Arjun Biswas

Share
10 |10000 characters needed characters left characters exceeded