deferReadiness for ember-cli


#1

In a globals-based Ember application, you can usually do:

var App = Ember.Application.createWithMixins({...});
App.deferReadiness();

But now with ember-cli you can not create the application: instead you have to extend and export:

var App = Ember.Application.extend({...});

export default App;

Since the application is not created, I can not deferReadiness here. How can this pattern be implemented for ember-cli based applications?

Thanks.

DanG


#2

My recommendation would be to use an Initializer:

/* global jQuery */
var Initializer = {

  name: 'initializeAsynchronously',

  initialize: function(container, app) {
    app.deferReadiness();
    jQuery.get('http://www.google.com/', function(data) {
      // Do stuff with data.
    }).always(function() {
      app.advanceReadiness();
    });
  }

};

export default Initializer;

#3

Makes sense, thanks!


#4

I can’t find the reference at the moment but someone from the core team told me you should never use deferReadiness in intializers. A better place for this sort of logic is in the model or setupController hooks in your application controller.


#5

One problem with deferReadiness is that it prevents the router from displaying a ‘loading’ state. The router does not proceed until the app has finished initialization. Potentially long-running XHR is not a good use case for an initializer and I would agree that the Route.model() hook is a better fit for this.


#6

@opsb Could you give an example, how to use deferReadiness in model & setupController in Ember CLI?


#7

in your routes/application.js you could do something lik the following:

export default Em.Route.extend({
    model: function () {
        return yourPreviousDeferReadinessLogic();
    }

This will stop the setupController execution until the yourPreviousDeferReadinessLogic promise resolves. If you previously had any other code inside the model hook you could do something like return yourPreviousDeferReadinessLogic().then(...)