cancel
Showing results for 
Search instead for 
Did you mean: 

sap.ui.commons.Label - getText() returns empty string

max_rosn
Participant
0 Kudos

Hi experts,

I have a label which is bound to a JSON Model:


var oModel1 = new sap.ui.model.json.JSONModel(data1);

var tokenText = new sap.ui.commons.Label({

text : "{/LoginResult/SessionToken}"

});

tokenText.setModel(oModel1);

alert(tokenText.getText());

The binding is working, the label is showing the token from the JSON Model. The label is placed in the content of the view.

However, the getText()-function of the Label is returning only an empty string.

Any ideas what might be wrong here? It's like the data binding is not actually done until the view is rendered or something like that?

Br

Max

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

can you try ,

  1. var oModel1 = new sap.ui.model.json.JSONModel(data1).attachRequestCompleted(function(){ 
  2. var tokenText = new sap.ui.commons.Label({ 
  3. text : "{/LoginResult/SessionToken}" 
  4. }); 
  5. tokenText.setModel(oModel1); 
  6. alert(tokenText.getText());
  7. });


max_rosn
Participant
0 Kudos

Hi Chandra,

I tried your suggestion with the same result. Empty string.

Sakthivel,

The alert does never show when I code like this.

I'm attaching the code here, thanks for all your help guys! This is driving me crazy.

Br

Max


sap.ui

  .jsview(

  "view.Menu",

  {

  getControllerName : function() {

  return "view.Menu";

  },

  createContent : function(oController) {

  var data =

  $.ajax({

  type : 'POST',

  url: "myURL",

  data: JSON.stringify({

  Username:"user",

  Password:"pw"}),

  dataType : "json",

  contentType: "application/json",

  success : function(data, textStatus, jqXHR) {

  oModel1.setData(data);

  },

  error : function(jqXHR, textStatus,

  errorThrown) {

  alert("errorcontroller");

  }

  });

  var tokenText = new sap.ui.commons.Label({

  });

  var oModel1 = new sap.ui.model.json.JSONModel(data).attachRequestCompleted(function(){

  tokenText.bindValue("/LoginResult/SessionToken");

  tokenText.setModel(oModel1);

  alert(tokenText.getText());

  });

  return new sap.m.Page({

  title : "Menu",

  enableScrolling : false,

  content : [ tokenText ]

  });

  }

  });

Former Member
0 Kudos

What you had previously tried had become complex with all those asynchronous callbacks ! Try something like this, and also define your tokenText before you do a ajax request.

  1. success : function(data, textStatus, jqXHR) { 
  2. var oModel1 = new sap.ui.model.json.JSONModel(data);
  3. tokenText.bindValue("/LoginResult/SessionToken"); 
  4. tokenText.setModel(oModel1); 
  5. alert(tokenText.getText());
  6. }
ChandraMahajan
Active Contributor
0 Kudos

set the model to core in view as mentioned below,

      sap.ui.getCore().setModel(oModel1);



Just change var tokenText = new sap.ui.commons.Label({  });

to  var tokenText = new sap.ui.commons.Label('myLable');

means provide id to your lable and then in controller.js file put the code that I mentioned to access the text.


Regards,

Chandra 

former_member206705
Participant
0 Kudos

Hi Max,

I would suggest you to make the createContent method look like as follows :


createContent : function(oController) {

     var tokenText = new sap.ui.commons.Label("label", {

          text : "{/LoginResult/SessionToken}"

     }); 

return new sap.m.Page({ 

       title : "Menu"

       enableScrolling : false

       content : [ tokenText ] 

  }); 

  } 


/* the following block of code can be put in the onInit method of the controller */

     var oModel1 = new sap.ui.model.json.JSONModel();

     var tokenText = this.getView().byId("label");

     tokenText.setModel(oModel1);

     oModel1.attachRequestCompleted(function(oEvent){

          alert(tokenText.getText());

     });


     var data =  $.ajax({ 

                           type : 'POST'

                           url: "myURL"

                           data: JSON.stringify({ 

                                Username:"user"

                                Password:"pw"}), 

                           dataType : "json"

                           contentType: "application/json"

                           success : function(data, textStatus, jqXHR) { 

                                   oModel1.setData(data); 

                           }, 

                           error : function(jqXHR, textStatus, errorThrown) { 

                                alert("errorcontroller"); 

                              } 

     }); 

/*code block ends here*/


max_rosn
Participant
0 Kudos

Hi Shilpa,

I tried that now and it seems like the ID is not recognized by the controller.

The following error occurs on the line tokenText.setModel(oModel1);

Uncaught TypeError: Cannot read property 'setModel' of undefined


The ID is recognized in the view but not in the controller. Am I missing something here?

Thanks!

Br

Max


former_member206705
Participant
0 Kudos

Hi Max,

Use sap.ui.getCore().byId("label") instead of this.getView().byId("label')

BR,

Shilpa

max_rosn
Participant
0 Kudos

Hi Shilpa,

thank you, that worked.

However, the alert is never executed.

If I remove the attachRequestCompleted()-function, it does (same result, empty string).

So I guess the request is not completed somehow? But in the end the JSON result is there anyway.

Br

Max

Former Member
0 Kudos

HI Max,

attachRequestCompleted() method would never execute as per your scenario, as you don't create any request there to load data .  If you need the requestComplete method to be triggered, then you'll have to load data as


oModel1.loadData("your-service-url");

OR


var oModel1 = new sap.ui.model.json.JSONModel("your-service-url");

So, when you do any of the above, a request is made to load data and once done, the requestCompleted call back is triggered.

Regards

Sakthivel

former_member206705
Participant
0 Kudos

Hi Max,

Instead of the attachRequestCompleted method, could you try calling the alert from the success handler of the ajax call, after oModel1.setData(data) ? You could instead, call a method from this point (after the line oModel1.setData(data) ) which alerts the label's text. This will help you do additional processig as well.

Best regards,

Shilpa

max_rosn
Participant
0 Kudos

Hi all,

thanks for your help.

This is now solved with putting the code in the success function of the ajax call.

I guess there were a problem with the asynchronous calls somehow. The alert function were executed before the actual ajax call, even tough the ajax code were placed before the alert function.

I dont quite understand this but it's at least working now.

Thanks,

Br

Max

former_member206705
Participant
0 Kudos

Hi Max,

Yes, this is an issue with asynchronous execution. Ideally, things that you want to be performed after the call is made should be put in the success handler of the ajax call.

Thanks and best regards,

Shilpa

Answers (3)

Answers (3)

0 Kudos

Any ideas what might be wrong here? It's like the data binding is not actually done until the view is rendered or something like that?

This is exactly right. The onInit() method of the controller is invoked after the view has completed creating the Control Tree / DOM.

SandipAgarwalla
Active Contributor
0 Kudos

Interesting thread. Can you post the json model itself here? Is it from a local file or variable defined inline?

new sap.ui.model.json.JSONModel(data1); 

ChandraMahajan
Active Contributor
0 Kudos
max_rosn
Participant
0 Kudos

Hi Chandrashekhar,

my code is equal to your example. The difference is I have everything in my .view-file.

Should I maybe combine it with the controller somehow?

I cant see why it should not work in the view-file.

Br

Max

ChandraMahajan
Active Contributor
0 Kudos

I guess you are putting alert after return statement. Just put it before return and it should work.


alert(tokenText.getText());

  return tokenText;

Regards,

Chandra

max_rosn
Participant
0 Kudos

In the end of the view-file this is happening:


return new sap.m.Page({

  title : "Menu",

  enableScrolling : false,

  content : [ tokenText ]

  });

the alert is done before this.

On the screen, the tokenText has the value of the token in the JSON Model. The binding does work.

But the getText-method returns "".

Also, the oModel1.getJSON() returns only "", which is also weird? It should return the whole JSON string?

ChandraMahajan
Active Contributor
0 Kudos

As your are returning control from view, it is unable to reach the alert statement.

you can place this code in controller.


       onInit: function() {

              var lableText = sap.ui.getCore().byId("myLable").getText();         

              alert('my lable text ' + lableText);

             

       },


Regards,

Chandra