Skip to content

Commit d0cf702

Browse files
Clean up example controller and utilize new cordova deviceready promise service
1 parent 9099abd commit d0cf702

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

app/js/controllers/example.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

3-
import angular from 'angular';
4-
5-
function ExampleCtrl($scope, $cordovaDevice) {
3+
function ExampleCtrl($scope, CordovaService, $cordovaDevice) {
64

75
// ViewModel
86
const vm = this;
@@ -13,22 +11,20 @@ function ExampleCtrl($scope, $cordovaDevice) {
1311
vm.deviceReadyStatus ='Cordova not loaded';
1412
vm.deviceInfo = {};
1513

16-
vm.loadDeviceInfo = function(){
17-
$scope.$apply(function () { //lets make sure angular hears about our update
18-
vm.deviceReady = true;
19-
vm.deviceReadyStatus = 'Device Ready';
20-
21-
try {
22-
angular.isDefined($cordovaDevice.getDevice()); //unfortunately if the plugin is not installed calling this will cause fatal error
23-
vm.deviceInfo = $cordovaDevice.getDevice();
24-
}
25-
catch (e) {
26-
vm.deviceReadyStatus += ' - Plugin not installed, please run "cordova plugin add cordova-plugin-device"';
27-
}
28-
});
14+
let loadDeviceInfo = () => {
15+
vm.deviceReady = true;
16+
vm.deviceReadyStatus = 'Device Ready';
17+
18+
try {
19+
angular.isDefined($cordovaDevice.getDevice()); //unfortunately if the plugin is not installed calling this will cause fatal error
20+
vm.deviceInfo = $cordovaDevice.getDevice();
21+
}
22+
catch (e) {
23+
vm.deviceReadyStatus += ' - Plugin not installed, please run "cordova plugin add cordova-plugin-device"';
24+
}
2925
};
3026

31-
document.addEventListener('deviceready', vm.loadDeviceInfo, false);
27+
CordovaService.ready.then( () => loadDeviceInfo() );
3228
}
3329

3430
export default {

0 commit comments

Comments
 (0)