How to execute Ember.RSVP.all correctly in the ember startup loop

I am trying to keep a promise inside Ember.RSVP.all

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.RSVP.all([configuration]).then(function(response) {
            //do something with the response in here
        });
    }
});

But since my integration test is mocking the xhr w / out of the loop, the test failed with the expected error "You turned on the test mode, which turned off autorun of the run loop

So, I wrapped RSVP with simple ember.run, thus

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.run(function() {
            Ember.RSVP.all([configuration]).then(function(response) {
                //do something with the response in here
            });
        });
    }
});

But I still get the error for some odd reason. Please note: if I run later, this will be fine (this will not work, although I need to execute the asynchronous test code for this test to work correctly)

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.run.later(function() {
            Ember.RSVP.all([configuration]).then(function(response) {
                //do something with the response in here
            });
        });
    }
});

Here is my implementation of ajaxPromise -fyi

var ajaxPromise = function(url, type, hash) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
        hash = hash || {};
        hash.url = url;
        hash.type = type;
        hash.dataType = 'json';

        hash.success = function(json) {
            Ember.run(null, resolve, json);
        };

        hash.error = function(json) {
            Ember.run(null, reject, json);
        };

        $.ajax(hash);
    });
}

How can I wrap Ember.RVSP inside my ember start without throwing this error?

Update

here is my test setup (including my assistant)

document.write('<div id="ember-testing-container"><div id="wrap"></div></div>');
App.setupForTesting();
App.injectTestHelpers();

test("test this async stuff works", function() {
    visit("/").then(function() {
        equal(1, 1, "omg");
    });
});

, , , jquery-mockjax, run xhr mock ( , , , async , )

+4
2

, , , ,

:

, , ( ) jQuery jQXHR-, , 0 nextTurn . . .

ember , then jQXHR

: https://github.com/emberjs/data/blob/4bca3d7e86043c7c5c4a854052a99dc2b4089be7/packages/ember-data/lib/adapters/rest_adapter.js#L539-L541

, .

var ajaxPromise = function(url, type, hash) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
        hash = hash || {};
        hash.url = url;
        hash.type = type;
        hash.dataType = 'json';

        hash.success = function(json) {
            Ember.run(null, resolve, json);
        };

        hash.error = function(json) {
            if (json && json.then) { json.then = null } // this line

            Ember.run(null, reject, json);
        };

        $.ajax(hash);
    });
}

, , . ( ) Ember.ajax, .

, ember-: https://github.com/emberjs/data/blob/4bca3d7e86043c7c5c4a854052a99dc2b4089be7/packages/ember-data/lib/adapters/rest_adapter.js#L570-L586

+7

, , , 1 mockjax, .

https://github.com/kingpin2k/jquery-mockjax/commit/ccd8df8ed7f64672f35490752b95e527c09931b5

    // jQuery < 1.4 doesn't have onreadystate change for xhr
      if ($.isFunction(onReady)) {
        if (mockHandler.isTimeout) {
          this.status = -1;
        }
        Em.run(function () {
          onReady.call(self, mockHandler.isTimeout ? 'timeout' : undefined);
        });
      } else if (mockHandler.isTimeout) {
        // Fix for 1.3.2 timeout to keep success from firing.
        this.status = -1;
      }
+1

All Articles