Merge pull request #524 from mattlewis92/ng1-promise-tweak

fix(ng1): fail gracefully when angular 1 promises can't be retrieved
This commit is contained in:
Max Lynch 2016-09-07 10:07:53 -05:00 committed by GitHub
commit d8baf3a814

View File

@ -105,17 +105,30 @@ function callCordovaPlugin(pluginObj: any, methodName: string, args: any[], opts
} }
function getPromise(cb) { function getPromise(cb) {
const tryNativePromise = () => {
if (window.Promise) {
return new Promise((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.');
}
};
if (window.angular) { if (window.angular) {
let $q = window.angular.element(document.body).injector().get('$q'); let injector = window.angular.element(document.querySelector('[ng-app]') || document.body).injector();
return $q((resolve, reject) => { if (injector) {
cb(resolve, reject); let $q = injector.get('$q');
}); return $q((resolve, reject) => {
} else if (window.Promise) { cb(resolve, reject);
return new Promise((resolve, reject) => { });
cb(resolve, reject); } else {
}); console.warn('Angular 1 was detected but $q couldn\'t be retrieved. This is usually when the app is not bootstrapped on the html or body tag. Falling back to native promises which won\'t trigger an automatic digest when promises resolve.');
return tryNativePromise();
}
} else { } 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.'); return tryNativePromise();
} }
} }