Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

It should be possible to override handleResponse() in an ember-data adapter #211

Open
nelstrom opened this issue Dec 7, 2016 · 7 comments
Labels
Ember Data Mixin Issue relates to the Ember Data mixin that ships with `ember-ajax`

Comments

@nelstrom
Copy link

nelstrom commented Dec 7, 2016

Overriding handleResponse() in an ember-data adapter doesn't get called

I'm trying to get ember-data to use the ember-ajax service. I've run into a problem: one of my adapters overrides the handleResponse() method, but the method never seems to get called.

Here's what I've got. My app/adapters/applications.js:

import DS from 'ember-data';
import AjaxSupport from 'booking/mixins/ajax-support';

export default DS.RESTAdapter.extend(AjaxSupport);

The AjaxSupport mixin is copied directly from the ember-ajax source code.

My app/adapters/booking.js adapter extends the application adapter and attempts to override the handleResponse() method:

import ApplicationAdapter from './application';
import DS from 'ember-data';

export default ApplicationAdapter.extend({
  handleResponse(status, headers, payload) {
    if (status === 400) {
      let errors = this.normalizeErrorResponse(status, headers, payload);
      return new DS.InvalidError(errors);
    }
    return this._super(...arguments);
  }
});

The API I'm working with uses a 400 status code where a 422 would be more appropriate. I want to treat the 400 response as an InvalidError rather than a BadRequest. Also, I'm trying to return a DS.InvalidError instead of the InvalidError supplied by ember-ajax, because I want to trigger the extractErrors() method in the corresponding app/serializers/booking.js. (The ember-data store checks for an instance of DS.InvalidError)

I've been able to get the desired behaviour by overriding the handleResponse method in my app/services/ajax.js file, like this:

import AjaxService from 'ember-ajax/services/ajax';
import DS from 'ember-data';

export default AjaxService.extend({
  handleResponse(status, headers, payload) {
    if (status === 400) {
      let errors = this.normalizeErrorResponse(status, headers, payload);
      return new DS.InvalidError(errors);
    }
    return this._super(...arguments);
  }
});

This version of handleResponse() does get called, whereas the method in my custom adapter does not. Previously (when ember-data was not using the ember-ajax service), the handleResponse() method in my BookingAdapter was called when ajax requests were made to the /bookings endpoint. I was expecting that it would continue to work this way.

I've also attempted to override the isInvalidError method with the same results: it works when used in my AjaxService, but when used in my BookingAdapter the code never gets called.

@XaserAcheron
Copy link
Contributor

Good timing! One of the big topics in the last working group meeting was tuning up the mixin to ensure it's 1:1 compatible with ember-data's existing way of doing things. This indeed seems like an issue in that regard -- I'll be drafting up some related tests pretty soon, so this is definitely on the radar.

As an unrelated side note, it's possible to import the mixin from ember-ajax directly without copying the source, like so:

import AjaxSupport from 'ember-ajax/mixins/ajax-support';

@nelstrom
Copy link
Author

nelstrom commented Dec 7, 2016

Ok, I'm glad this is timely. I wasn't sure whether to log this as an issue on ember-ajax or ember-data.

Thanks for the extra pointer about the ajax-support mixin. That should simplify things nicely.

@alexlafroscia alexlafroscia added the Ember Data Mixin Issue relates to the Ember Data mixin that ships with `ember-ajax` label Jan 16, 2017
@CodeDeFury
Copy link

CodeDeFury commented Mar 5, 2018

hi, I'm having the same issue here. handleResponse on adapter is never called once I extend ajaxSupport. Is there any temporary solution that I can make it work?

@XaserAcheron @nelstrom

@sumeetattree
Copy link

Ran into this issue myself. Any work around other than what @nelstrom suggested?

#224 (comment)

@pankajparkar
Copy link

Any updates on this? Almost 3 years no proper solution on this 🤔

@c-emil
Copy link

c-emil commented Sep 18, 2019

@pankajparkar You have to override the ajax service. For example:

services/ajax.js

import AjaxService from 'ember-ajax/services/ajax';

export default AjaxService.extend({
  handleResponse() { return this._super(...arguments); },
});

@pankajparkar
Copy link

@c-emil Thanks, my handleResponse methods are not working on child level adapters :(
Any workaround for make child component level adapters working?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Ember Data Mixin Issue relates to the Ember Data mixin that ships with `ember-ajax`
Projects
None yet
Development

No branches or pull requests

7 participants