开发者

Determine if internet connection is available

how do you folks check if the 开发者_如何学Cdevice is connected to the internet in a sencha touch app?


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/.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜