Sencha Touch 2: Check connection state
Small ST2 util that lets you check the state of the Internet connection and execute function when the state changes.
app/util/Connection.js
Ext.define('Ext.util.Connection', {
singleton: true,
isOnline: function () {
return navigator.onLine;
},
on: function (state, fn, ctx, params) {
params = params || [];
fn = fn || Ext.emptyFn;
var single = params.single || false;
function eventListenerFn() {
fn.apply(ctx, arguments);
if (single) {
window.removeEventListener(state, eventListenerFn);
}
}
window.addEventListener(state, eventListenerFn);
}
});
app.js
Set the path of the file
Ext.Loader.setPath({
...
'Ext.util.Connection': './app/util/Connection.js'
});
Also require the file
require: [
...
'Ext.util.Connection'
]
How to use ?
Check wether the device has internet access:
Ext.util.Connection.isOnline()
Execute function when connection is lost
Ext.util.Connection.on('offline', this.lostConnection, this);
Execute function when connection is regained
Ext.util.Connection.on('online', this.regainedConnection, this);Written by Titouan de Bailleul
Related protips
2 Responses
Did you check out Ext.device.Connection.isOnline()?
Doesn't it do the same thing?
@sluzky It actually does, but the Ext.device.Connection.onlinechange event is not fired on desktop. You can only use this event with native packaging.
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside