Skip to content

Instantly share code, notes, and snippets.

@htulipe
Last active September 12, 2018 17:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save htulipe/44d899e56e2526a82e46 to your computer and use it in GitHub Desktop.
Save htulipe/44d899e56e2526a82e46 to your computer and use it in GitHub Desktop.
Wait for Phonegap's deviceready event before initializing an Ember application
import isOnCordova from '../utils/is-on-cordova';
export function initialize(container, application) {
application.deferReadiness();
document.addEventListener('deviceready', function() {
application.advanceReadiness();
}, false);
if(!isOnCordova()){
document.dispatchEvent(new Event('deviceready'));
}
}
export default {
name: 'cordova-device-ready',
initialize: initialize
};
export default function isOnCordova() {
return !!window.cordova;
}
@htulipe
Copy link
Author

htulipe commented Apr 12, 2015

This is all the code you need to run your Ember app inside PhoneGap/Cordova. I was pretty amazed how easy it is to plug in code inside the Ember framework.

cordova-device-ready.js is an Ember Initializer while is-on-cordova.js is a simple utility module.

The initializer defers the application "readiness" until the deviceready event is fired. If the code is not executed under Cordova (in the browser), the code dispatch the event itself.

@benjcal
Copy link

benjcal commented Jul 18, 2016

Awesome, thank you for this!

Two little things: L3 doesn't need container as an argument in the initialize function and in L15 can be just initialize

This is using ember 2.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment