PDA

View Full Version : How to get ajax request into a variable in sencha touch?



alexgv
21 Jun 2011, 9:29 AM
What I am trying to do is is get the information I have in my ajax request as the badgetext number
Here is where I am trying to do it. This seems simple, but I cant figure it out.





Ext.Ajax.request({
url: '../lib/messages.php',
success: function(response, opts) {
var badge_number = Ext.decode(response.responseText);
console.dir(badge_number);
},
failure: function(response, opts) {
console.log('server-side failure with status code');
}
});

var buttonsGroup1 = [{
text: 'Messages',
//badge_number variable from ajax request would go here. badgeText: '2',
handler: tapHandler
}];




I just have the php script returning a number. The server responds fine and the success function works it retrieves the data. But I want the data that the success function gets from the php script to be the badgeText. My problem is that I do not know how to put that data into badgeText which is in the variable buttonsGroup1

Chirieac
21 Jun 2011, 9:53 AM
You could do something like this in the ajax success function:


success: function(response, opts) {
var badge_number = Ext.decode(response.responseText);
buttonsGroup1[0].badgeText = badge_number;
},

alexgv
21 Jun 2011, 9:55 AM
Is this way better?


Ext.Ajax.request({
url: '../lib/messages.php',
success: function(response, opts) {
buttonsGroup1.setBadge(Ext.decode(response.responseText));
},
failure: function(response, opts) {
console.log('server-side failure with status code');
}
});

var buttonsGroup1 = new Ext.Button({
text: 'Messages',
handler: tapHandler
});

jjerome
21 Jun 2011, 10:02 AM
Try this out:
Create a global variable called badgeText with your projectname before it (shown below). Just switch 'PROJECTNAME' with your project folder's name.

That usually works for me.

Good luck! :)

Look for my changes (orange commented text) below:



//PROJECTNAME.badgeText;
Ext.Ajax.request({
url: '../lib/messages.php',
success: function(response, opts) {
//PROJECTNAME.badgeText= Ext.decode(response.responseText);
//console.dir(PROJECTNAME.badgeText);
},
failure: function(response, opts) {
console.log('server-side failure with status code');
}
});

var buttonsGroup1 = [{
text: 'Messages',
//badgeText: PROJECTNAME.badgeText,
handler: tapHandler
}]; If you aren't working on this in a project, try just creating a regular global variable to store the badgeText.

jacobnelson
13 Mar 2013, 3:48 AM
I got this example from Sencha tutorial


var myRequest = Ext.Ajax.request (http://docs.sencha.com/touch/2-1/#!/api/Ext.Ajax-method-request)({ url: 'myUrl', failure: function(response) { console.log(response.aborted); // logs true } });

try this out.

http://jnelson.in