how do you folks check if the device is connected to the internet in a sencha touch app?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
There's an attribute called navigator.onLine
(general browser support, not specific for Sencha)
If I'm in a PhoneGap application (which you often are if you're using Sencha Touch), I'd rather use their network.isReachable function, since I've by experience found it more reliable.
There's also something called 'Offline events', John Resig describes them on his blog: http://ejohn.org/blog/offline-events/.
回答2:
If there is no network connection during an ajax request sencha touch will return a response with a 0 status. So this is what we're using in our app (works in phonegap too):
// global error handling
Ext.Ajax.on('requestexception', function(connection, response) {
// get rid of any loading masks that are present
Ext.each(Ext.query('.x-mask'), function(el) {
new Ext.Element(el).hide()
});
switch(response.status) {
case 0 :
Ext.Msg.alert('Error Loading', 'No Internet connection.');
break;
case 401 :
Ext.Msg.alert('Login Failed', 'Check your username and password.');
break;
default :
Ext.Msg.alert('Whoops!', 'An unexpected error has occurred and we have been alerted.');
}
});
Note that this is for touch 1.1, haven't tried in in 2.0 yet.