on 04-28-2014 10:49 AM
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
can you try ,
- var oModel1 = new sap.ui.model.json.JSONModel(data1).attachRequestCompleted(function(){
- var tokenText = new sap.ui.commons.Label({
- text : "{/LoginResult/SessionToken}"
- });
- tokenText.setModel(oModel1);
- alert(tokenText.getText());
- });
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ]
});
}
});
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.
- success : function(data, textStatus, jqXHR) {
- var oModel1 = new sap.ui.model.json.JSONModel(data);
- tokenText.bindValue("/LoginResult/SessionToken");
- tokenText.setModel(oModel1);
- alert(tokenText.getText());
- }
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
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*/
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
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
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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
See this example JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="h...
Regards,
Chandra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
User | Count |
---|---|
80 | |
9 | |
9 | |
7 | |
7 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.