From 046ad53b20dcbcfdbc5f48e63975920486de596f Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 16 Sep 2014 08:55:00 -0500 Subject: [PATCH] feat(cordovaEvents): $ionicPlatform.on method Create $ionicPlatform.on(type, callback) to make it easier to add Cordova event listeners. Closes #2219 --- js/angular/service/platform.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/js/angular/service/platform.js b/js/angular/service/platform.js index 71a7ac03af..3f16b1db29 100644 --- a/js/angular/service/platform.js +++ b/js/angular/service/platform.js @@ -177,6 +177,28 @@ IonicModule return ionic.Platform.is(type); }, + /** + * @ngdoc method + * @name $ionicPlatform#on + * @description + * Add Cordova event listeners, such as `pause`, `resume`, `volumedownbutton`, `batterylow`, + * `offline`, etc. More information about available event types can be found in + * [Cordova's event documentation](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events). + * @param {string} type Cordova [event type](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events). + * @param {function} callback Called when the Cordova event is fired. + * @returns {function} Returns a deregistration function to remove the event listener. + */ + on: function(type, cb) { + ionic.Platform.ready(function(){ + document.addEventListener(type, cb, false); + }); + return function() { + ionic.Platform.ready(function(){ + document.removeEventListener(type, cb); + }); + }; + }, + /** * @ngdoc method * @name $ionicPlatform#ready