travis-web/app/adapters/application.js
Piotr Sarnacki 5dc64c4937 Fix findRecord to work without passing a snapshot
We use adapter.findRecord to fetch repo by slug. Using it this way was
broken with one of the ember-data releases. This commit brings back the
old behaviour.
2016-03-07 13:32:49 +01:00

58 lines
1.7 KiB
JavaScript

import config from 'travis/config/environment';
import Ember from 'ember';
import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend({
auth: Ember.inject.service(),
host: config.apiEndpoint,
coalesceFindRequests: true,
// Before Ember Data 2.0 the default behaviour of running `findAll` was to get
// new records only when there're no records in the store. This will change
// to a different strategy in 2.0: when you run `findAll` it will not get any
// new data initially, but it will try loading new data in the background.
//
// I'm disabling the new behaviour for now.
shouldBackgroundReloadRecord() {
return false;
},
ajaxOptions(url, type, options) {
var base, hash, token;
hash = this._super(...arguments);
hash.headers = hash.headers || {};
hash.headers['accept'] = 'application/json; version=2';
if (token = this.get('auth').token()) {
if(!hash.headers['Authorization']) {
hash.headers['Authorization'] = "token " + token;
}
}
return hash;
},
findMany(store, type, ids) {
return this.ajax(this.buildURL(type.modelName), 'GET', {
data: {
ids: ids
}
});
},
handleResponse(status, headers, payload) {
if (status > 299) {
console.log("[ERROR] API responded with an error (" + status + "): " + (JSON.stringify(payload)));
}
return this._super(...arguments);
},
// this can be removed once this PR is merged and live:
// https://github.com/emberjs/data/pull/4204
findRecord(store, type, id, snapshot) {
return this.ajax(this.buildURL(type.modelName, id, snapshot, 'findRecord'), 'GET');
}
});