Is it possible to create a sap.ui.controller like that:
sap.ui.controller("controllerId", "{onAfterRendering: function(){}}");
Use-Case is, i have a server that creates a controller on the fly, if needed.
When I get the Logic as String and try to use it like that:
var oView = sap.ui.view({
type: sap.ui.core.mvc.ViewType.JSON,
viewContent: viewContent,
controller: sap.ui.controller("controllerId", "{onAfterRendering: function(){}}")
});
the Controller is always undefined.
Any hint on that topic?
EDIT 2:
After Timothy Groote updated answer:
Yes, i send a AJAX-Call to the Server. I have tried that right now:
var ajaxAnswer = 'sap.ui.controller("testForm", { oView: null, onAfterRendering: function(oEvent){oView = oEvent.getSource();} });'
eval('var controller =' + ajaxAnswer);
But controller
is undefined.
EDIT:
According to the Answer, i think i need to make it even clearer:
This part "{onAfterRendering: function(){}}"
comes from the Server as a String. I can't remove any quotes from that String. I could parse it with JSON.parse()
, but that can't parse a function.
After I reported an Issue on github/SAP/openui5, they showed the right way to implement a sap.ui.controller. This is a JSBin Example
You should just eval() the ajax answer:
and then let the View instantiate the controller:
Due to the Documentation sap.ui.controller returns void or sap.ui.core.mvc.Controller, depending on the use case.
sap.ui.controller is always instantiated by a View with the controller name. If you create a
sap.ui.controller("<Insert Name Here>", code Implementation)
you can instantiate it in your View with
sap.ui.controller("<Insert Same Name As Above>")
.