mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
feat(plugin): Network Information and global offline/online events
This commit is contained in:
@ -50,6 +50,23 @@ export class IonicApp {
|
||||
this.components = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind some global events and publish on the 'app' channel
|
||||
*/
|
||||
bindEvents(events) {
|
||||
window.addEventListener('online', (event) => {
|
||||
events.publish('app:online', event);
|
||||
}, false);
|
||||
|
||||
window.addEventListener('offline', (event) => {
|
||||
events.publish('app:offline', event);
|
||||
}, false);
|
||||
|
||||
window.addEventListener('orientationchange', (event) => {
|
||||
events.publish('app:rotated', event);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* @param {Object} appRef TODO
|
||||
@ -302,6 +319,8 @@ export function ionicBootstrap(rootComponentType, views, config) {
|
||||
let translate = new Translate();
|
||||
let navRegistry = new NavRegistry(views);
|
||||
|
||||
app.bindEvents(events);
|
||||
|
||||
// add injectables that will be available to all child components
|
||||
let appBindings = Injector.resolve([
|
||||
bind(IonicApp).toValue(app),
|
||||
|
46
ionic/platform/network/network.ts
Normal file
46
ionic/platform/network/network.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import * as Rx from 'rx';
|
||||
|
||||
import * as util from 'ionic/util';
|
||||
import {NativePlugin} from '../plugin';
|
||||
|
||||
/**
|
||||
* Access Network information and respond to changes in network state.
|
||||
*
|
||||
* @usage
|
||||
* ```js
|
||||
* let networkInfo = Network.getNetwork()
|
||||
* let isOnline = Network.isOnline()
|
||||
* let isOffline = Network.isOffline()
|
||||
* ```
|
||||
*/
|
||||
@NativePlugin({
|
||||
name: 'Network',
|
||||
platforms: ['ios', 'android'],
|
||||
engines: {
|
||||
cordova: 'cordova-plugin-network-information'
|
||||
},
|
||||
pluginCheck: () => {
|
||||
return !!navigator.connection;
|
||||
}
|
||||
})
|
||||
@Injectable()
|
||||
export class Network {
|
||||
/**
|
||||
* Return network info.
|
||||
*/
|
||||
static info() {
|
||||
this.ifPlugin(() => {
|
||||
return navigator.connection.type;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the device is online
|
||||
*/
|
||||
static isOnline() {
|
||||
this.ifPlugin(() => {
|
||||
var networkState = navigator.connection.type;
|
||||
return networkState !== window.Connection.UNKNOWN && networkState !== window.Connection.NONE;
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user