PDA

View Full Version : Create sencha button that will poll xml to represent current status



dustout
24 Aug 2011, 1:45 PM
I am wanting to create a button on a form that will constantly poll an xml source, and update the text on a button depending on what the xml returns.

jjerome
25 Aug 2011, 11:12 AM
you mean like setting the badgeText?

dustout
25 Aug 2011, 12:30 PM
Kind of, but i would like to have it constantly update without requiring the user to refresh the page

jjerome
25 Aug 2011, 1:04 PM
Usually things like that require users to refresh pages. Do you ever call doLayout() after you update the text on the button? I don't think that should be an issue though.

dustout
25 Aug 2011, 1:09 PM
lol, yea it seems as if every example i have found involves refreshing the page in one way or another.

I can't say i remember calling doLayout, i'll see what i can do, but the more i spend on this the more i am starting to think that i'll have to hack something together

jjerome
25 Aug 2011, 1:48 PM
Good luck!

dustout
25 Aug 2011, 1:54 PM
Thanks, i'll need it \:D/

NickT
26 Aug 2011, 5:12 PM
you could extend button to create a polling button. you could then use the DelayedTask object in sencha touch. Here is a first cut (untested).



Ext.namespace('Ext.ux');


Ext.ux.PollingButton = Ext.extend(Ext.Button, {
pollingInterval: 5000,
listeners: {
'render': function(button) {
if (button.pollingInterval > 0) {
button.delayedTask.delay(button.pollingInterval);
}
}
},
initComponent: function() {
this.delayedTask = new Ext.util.DelayedTask(this.executePoll, this);
Ext.ux.PollingButton.superclass.initComponent.call(this);
},
executePoll: function() {
Ext.Ajax.request({
url: 'your url here',
method: 'POST',
scope: this,
jsonData: {whatever you want to send down to the server},
success: function(response, opts) {
// TODO: process your response.
var text = 'set the text based on your xml response';
this.setText(text);
if (this.pollingInterval > 0) {
this.delayedTask.delay(this.pollingInterval);
}
},
failure: function(response, opts) {
console.log('your error message');
// TODO: decide if you want to reschedule the polling to execute depending on the error
}
});
}
});
Ext.reg('pollingbutton', Ext.ux.PollingButton);