poetic/ember-cli-cordova

No need for device ready?

sankalpsingha opened this issue · 10 comments

Is there no need for the Cordova Device ready? If so, where do we put it? Do we need to create an initializer for it?

You do need deviceReady before accessing Cordova APIs or you will hit unexpected behavior. A common pattern is to use an initializer to wait for Cordova to complete setup.

For instance, mine looks like this.

/*global document, cordova, StatusBar*/

//listeners to adjust the view size
function keyboardShowHandler(e) {
  document.body.style.height = "calc(100% - " + e.keyboardHeight + "px)"; }
function keyboardHideHandler() { document.body.style.height = ""; }

export default {
  name: "cordova",
  before: "session",

  initialize: function (registry, application) {

    if (window.cordova) {
        application.deferReadiness();
    }
    document.addEventListener('deviceready', function() {

      application.advanceReadiness();

      cordova.plugins.Keyboard.disableScroll(true);
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      window.addEventListener('native.keyboardshow', keyboardShowHandler);
      window.addEventListener('native.keyboardhide', keyboardHideHandler);

      StatusBar.backgroundColorByHexString('#ffffff');
      StatusBar.overlaysWebView(false);
      StatusBar.styleDefault();
      StatusBar.show();

    }, false);
  }
};

Thanks for the information. I had figured that we would have to do something like this, however I think that this info should also be noted in the README. I think that this is a really critical information specially for 'CORDOVA' application. Or maybe having this as boilerplate in the some generator?

@runspired thanks for the tip.

@sankalpsingha yeah this would be useful info to see in the readme

I should add that now that ember has instance-initializers I'm using a new pattern that avoids using deferReadiness entirely. The splash screen is removed by the application route, which also provides the advantage that the ember boots, loads, and does the initial render while cordova is still finishing up, shaving time off of your boot :)

hashg commented

@runspired can you share the same code with using instance-initializers also provide info about the file path in cli for placing these new intializers.

@runspired yeah could you provide us with an example? that'd be great.

relevant article: http://hussfelt.net/2015/08/06/how-to-stop-using-deferreadiness-and-advancereadiness-in-ember/

tl;dr — instead of using an initializer and calling deferReadiness (which is dangerous, if you ever forget to advance readiness (like i just did, running tests in a non-cordova environment)), or using an instance initializer (because those "don't participate in the router promise chain"), he advocates making your setup code return a promise, and then return that from your ApplicationRoute's beforeModel hook.

This way, you immediately render the application template, and take advantage of loading / error routes while your setup promise resolves. I'm going to give this a shot.

@runspired could you please provide details on how you're using the new instance-initializers?

@rbartoli its detailed in the post above

@runspired perfect thanks, I had totally missed that link/post.