-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathjson-api-adapter.js
84 lines (72 loc) · 2.01 KB
/
json-api-adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
@module ember-data
*/
import RESTAdapter from "ember-data/adapters/rest-adapter";
/**
@class JSONAPIAdapter
@constructor
@namespace DS
@extends DS.RESTAdapter
*/
export default RESTAdapter.extend({
defaultSerializer: '-json-api',
/**
@method ajaxOptions
@private
@param {String} url
@param {String} type The request type GET, POST, PUT, DELETE etc.
@param {Object} options
@return {Object}
*/
ajaxOptions: function(url, type, options) {
let hash = this._super(...arguments);
if (hash.contentType) {
hash.contentType = 'application/vnd.api+json';
}
let beforeSend = hash.beforeSend;
hash.beforeSend = function(xhr) {
xhr.setRequestHeader('Accept', 'application/vnd.api+json');
if (beforeSend) {
beforeSend(xhr);
}
};
return hash;
},
/**
@method findMany
@param {DS.Store} store
@param {DS.Model} type
@param {Array} ids
@param {Array} snapshots
@return {Promise} promise
*/
findMany: function(store, type, ids, snapshots) {
var url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
return this.ajax(url, 'GET', { data: { filter: { id: ids.join(',') } } });
},
/**
@method pathForType
@param {String} modelName
@return {String} path
**/
pathForType: function(modelName) {
var dasherized = Ember.String.dasherize(modelName);
return Ember.String.pluralize(dasherized);
},
// TODO: Remove this once we have a better way to override HTTP verbs.
/**
@method updateRecord
@param {DS.Store} store
@param {DS.Model} type
@param {DS.Snapshot} snapshot
@return {Promise} promise
*/
updateRecord: function(store, type, snapshot) {
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, snapshot, { includeId: true });
var id = snapshot.id;
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(url, 'PATCH', { data: data });
}
});