1

I am attempting to use ember-cli and ember-cli-ic-ajax for Ember Data fixtures. I have imported ic.ajax.defineFixture as icDefineFixture.

icDefineFixture('/users', {
  response: {
    email: 'user@example.com'
  },
  textStatus: 'success',
  jqXHR: {}
});

Which doesn't throw any errors, so I know I'm importing the function successfully. I then create a record, which goes okay:

this.store.createRecord('user', {});

But when I try and save it in a controller action:

this.get('model').save().then(function() { ... });

I get 404 errors because ember data is contacting my server which doesn't have a backend when I'm testing.

I'm puzzled how Ember Data is supposed to know to use ic-ajax's ajax methods. Is this something I must manually set up? I haven't managed to find any documentation on it.

Thanks!

Kevin Bullaughey
  • 2,556
  • 25
  • 37

3 Answers3

1

I don't believe that ember-data uses ic-ajax at all, but jQuery.ajax. In the app I'm currently writing, I'm using ic-ajax instead of $.ajax, simply because it returns RSVP promises. Now, I'm making requests without ember-data.

Here's an example from the Ghost source where ic-ajax has been used to make requests.

Setup: https://github.com/TryGhost/Ghost/blob/master/core/client/utils/ajax.js
Usage: https://github.com/TryGhost/Ghost/blob/master/core/client/controllers/setup.js#L23

Regarding fixtures, from the ic-ajax Github readme:

Adding fixtures with defineFixture tells ic-ajax to resolve the promise with the fixture matching a url instead of making a request. This allows you to test your app without creating fake servers with sinon, etc.

Example:

ic.ajax.defineFixture('api/v1/courses', {
  response: [{name: 'basket weaving'}],
  jqXHR: {},
  textStatus: 'success'
});

ic.ajax.request('api/v1/courses').then(function(result) {
  deepEqual(result, ic.ajax.lookupFixture('api/v1/courses').response);
});

Now, if you want to use ic-ajax with ember-data, you could override the createRecord, deleteRecord methods on the Adapter you're using. See this Ember API document about createRecord for an example of how ember-data makes ajax calls: http://emberjs.com/api/data/classes/DS.Adapter.html#method_createRecord

ToddSmithSalter
  • 715
  • 6
  • 20
1

Thanks to @ToddSmithSalter for the suggestion which led me to a solution that turned out to work very well for my case. I was able to simply reopen my adapter in a file that's only loaded in my test suite and override the ajax method:

ApplicationAdapter.reopen({
  ajax: function(url, verb, hash) {
    icRequest(url)
  }
});

Where I have imported ic.ajax.request as icRequest

This seems to work fine, but I don't see a way to register fixtures separately for each type of request PUT, GET, POST, DELETE, etc.

Kevin Bullaughey
  • 2,556
  • 25
  • 37
1

Thanks to @kevin-bullaughey comment I was able to adjust the solution for Ember CLI 0.2.0-beta.1:

import DS from 'ember-data';

import ENV from '../config/environment';
import { request } from 'ic-ajax';

var ApplicationAdapter = DS.RESTAdapter.extend();

if (ENV.environment === "test") {
    ApplicationAdapter.reopen({
        ajax: function(url) {
            return request(url);
        }
    });
}

export default ApplicationAdapter;

In the test environment the adapter will use the defined ic-ajax fixtures and behave unchanged in development or production.

Christopher
  • 106
  • 3