George Anderson
George Anderson

Reputation: 992

Defer ember.js app readiness until initial ajax call returns

(using Ember 1.0.pre)

When my Ember.js app first loads for a given user, I'd like to make a single ajax call and not load any ember-data-related data until that single call returns. Here's what I think I need to do, but I'm not sure how to implement it:

I don't know how or where I need to call deferReadiness(). This is what I've tried and its result:

SocialRoi = Ember.Application.create({
  ready: function() {
    this.deferReadiness();
    console.log('SocialRoi almost ready...');
    this.advanceReadiness();
    return this._super();
  }
});

// RESULT:
// Uncaught TypeError: Object SocialRoi has no method 'deferReadiness'

Any ideas what I'm doing wrong? Is there a better approach to accomplish my goal? Thanks.

Upvotes: 4

Views: 2131

Answers (2)

dysbulic
dysbulic

Reputation: 3105

I get an error when calling initialize. I needed to delay the start of my Ember app running in Cordova until Couchbase Lite starts. That looks like:

App = Ember.Application.create()

App.deferReadiness()
⋮
;( function() {
  function checkURL() {
    if( ! window.cblite ) {
      console.error( 'couchbase lite not present' )
    } else {
      cblite.getURL( function( err, url ) {
        var adapter = App.__container__.lookup('store:main').adapterFor( 'application' )
        url = url.substring( 0, url.length - 1 )
        Ember.set( adapter, 'host', url )

        App.advanceReadiness()
        ⋮

Upvotes: 0

pjlammertyn
pjlammertyn

Reputation: 989

You could do something like this, ex for using Ember together with SignalR.

APP = Ember.Application.create({
  appname: "MyEmberApp",
  ready: function () {
    console.log("hello from ember");

    if (Ember.empty($.connection.hub) || !$.connection.hub.stateChanged) {
      alert('SignalR error');
      return;
    }
  }
});

APP.deferReadiness();
$.connection.hub.url = 'http://localhost:8085/signalr';
$.connection.hub.logging = true;
$.connection.hub.start()
  .done(function () {
    console.log("SignalR started!");
    APP.advanceReadiness();
  })
  .fail(function () {
    alert('SignalR error)';
  });

APP.Router = Ember.Router.extend({
  ...
});

APP.initialize();

Upvotes: 4

Related Questions