Skip to Content
avatar image
Former Member

Create controller function at runtime

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Feb 13 at 10:39 AM

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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 13 at 12:17 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 13 at 05:17 AM

    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

    Add comment
    10|10000 characters needed characters exceeded