Skip to Content
Aug 09, 2012 at 10:24 AM

Help with accessing View Content from Controller



I have been playing around a bit with the MVC structure for SAPUI5 projects and have come a bit stuck! I am hoping someone can help me out. It seems like a really stupid question so my apologies upfront but I can't work out what's going on so am hoping someone else can see what I am doing wrong.

Here is the scenario:

  • I created a new SAPUI5 Application Project using the Eclipse Wizard. I added one View using the wizard of type JS.

  • Then in that View I added a very simple Text View and set the text to "Set in View"

sap.ui.jsview("mvc101.view1", {      getControllerName : function() {         return "mvc101.view1";      },      createContent : function(oController) {                var oTextView1 = new sap.ui.commons.TextView("tView1");                oTextView1.setText("Set in View");                return oTextView1;      }});

  • Then in the controller I try to access the Text View as follows and set the text to be "Set in Controller"

onInit: function() {             this.byId("tView1").setText("Set in Controller");   },

But when I run and debug this I get an error in the console saying that this.byId("tView1") is undefined:

However when I use firebug I can see tView1 in the content aggregation of the View:

Help... What am I doing wrong here? Can anyone see my mistake?



Update: When I use a JSON view type as follows it works fine (no undefined errors) to access the TextView using the this.byId() method:


{    "Type":"sap.ui.core.mvc.JSONView",    "controllerName":"mvc101.view2",    "content": [{              "Type" : "sap.ui.commons.TextView",              "id" : "tView2",              "text" : "Set in View2"   }]}

And the onInit method of the Controller:

onInit: function() {             this.byId("tView2").setText("Set in Controller2");   }