mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-04-13 00:00:10 +08:00
ng1 magic
This commit is contained in:
+25
-1
@@ -1,7 +1,10 @@
|
||||
import {get} from '../util';
|
||||
|
||||
import {publishAngular1Service} from '../ng1';
|
||||
|
||||
declare var window;
|
||||
declare var Promise;
|
||||
declare var $q;
|
||||
|
||||
import {Observable} from '@reactivex/rxjs/dist/cjs/Rx';
|
||||
|
||||
@@ -74,8 +77,25 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
|
||||
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
|
||||
}
|
||||
|
||||
function getPromise(cb) {
|
||||
if(window.Promise) {
|
||||
console.log('Native promises available...');
|
||||
return new Promise((resolve, reject) => {
|
||||
cb(resolve, reject);
|
||||
})
|
||||
} else if(window.angular) {
|
||||
let $q = window.angular.injector(['ng']).get('$q');
|
||||
console.log('Loaded $q', $q);
|
||||
return $q((resolve, reject) => {
|
||||
cb(resolve, reject);
|
||||
});
|
||||
} else {
|
||||
console.error('No Promise support or polyfill found. To enable Ionic Native support, please add the es6-promise polyfill before this script, or run with a library like Angular 1/2 or on a recent browser.');
|
||||
}
|
||||
}
|
||||
|
||||
function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return getPromise((resolve, reject) => {
|
||||
callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
||||
})
|
||||
}
|
||||
@@ -117,6 +137,10 @@ export function Plugin(config) {
|
||||
cls[k] = config[k];
|
||||
}
|
||||
|
||||
cls['installed'] = function() {
|
||||
return !!getPlugin(config.pluginRef);
|
||||
}
|
||||
|
||||
return cls;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user