PDA

View Full Version : PhoneGap onlinechange Event - A solution



Kurt001
12 May 2013, 7:37 AM
Hi there,

if you do a native build with Sencha you can listen to the 'onlinechange event'.

With PhoneGap you either ask for the current status or you have to add your own solution.
I have written a few lines to add to 'Ext.device.connection.PhoneGap' and you have the same event being fired.



Ext.define('Ext.device.connection.PhoneGap', {
extend: 'Ext.device.connection.Abstract',


constructor: function() {
this.callSuper(arguments);
document.addEventListener("online", Ext.bind(this.onConnectionChange, this, [true]), false);
document.addEventListener("offline", Ext.bind(this.onConnectionChange, this, [false]), false);
},


onConnectionChange: function(online) {
this.fireEvent('onlinechange', online);
},


syncOnline: function() {
...


For more detailed information you can exchange the 'online' param for.
this.getOnline(), this.getType()

Add this to app.js launch:


Ext.device.Connection.on('onlinechange', this.onlineCallback);

onlineCallback: function(online){
// Do your stuff
}

steve1964
16 May 2013, 8:29 AM
????, you have the online / offline events in Phonegap too:
http://docs.phonegap.com/en/2.7.0/cordova_events_events.md.html#Events

Kurt001
16 May 2013, 9:50 AM
Exactly - that's what I implemented, so that you do not have to add this to each project again and again. Thats whay I am adding it to Ext.device.connection.PhoneGap.

If you look at line 2 and 3 of the constructor you will see the PhoneGap listeners.
If you have the skill to add it on your own and you prefer to add it seperately, there is not need for you to use it.

But for convenient reasons I added them for everyone in the place where Sencha handles these things.

I hope it still helps :)