2019-06-21 21:05:45 +00:00
|
|
|
import BaseAdapter from './base';
|
2019-07-02 21:23:07 +00:00
|
|
|
import { decamelize } from '@ember/string';
|
|
|
|
import { getProperties } from '@ember/object';
|
2019-06-21 21:05:45 +00:00
|
|
|
|
|
|
|
export default BaseAdapter.extend({
|
|
|
|
createRecord(store, type, snapshot) {
|
|
|
|
let name = snapshot.id || snapshot.attr('name');
|
|
|
|
let url = this._url(
|
|
|
|
type.modelName,
|
|
|
|
{
|
|
|
|
backend: snapshot.record.backend,
|
|
|
|
scope: snapshot.record.scope,
|
|
|
|
},
|
|
|
|
name
|
|
|
|
);
|
2019-06-25 20:57:50 +00:00
|
|
|
return this.ajax(url, 'POST', { data: this.serialize(snapshot) }).then(() => {
|
2019-06-21 21:05:45 +00:00
|
|
|
return {
|
|
|
|
id: name,
|
|
|
|
name,
|
2019-07-02 21:23:07 +00:00
|
|
|
backend: snapshot.record.backend,
|
|
|
|
scope: snapshot.record.scope,
|
2019-06-21 21:05:45 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-07-02 21:23:07 +00:00
|
|
|
deleteRecord(store, type, snapshot) {
|
|
|
|
let name = snapshot.id || snapshot.attr('name');
|
|
|
|
let url = this._url(
|
|
|
|
type.modelName,
|
|
|
|
{
|
|
|
|
backend: snapshot.record.backend,
|
|
|
|
scope: snapshot.record.scope,
|
|
|
|
},
|
|
|
|
name
|
|
|
|
);
|
|
|
|
return this.ajax(url, 'DELETE');
|
|
|
|
},
|
|
|
|
|
2019-06-25 20:57:50 +00:00
|
|
|
serialize(snapshot) {
|
|
|
|
// the endpoint here won't allow sending `operation_all` and `operation_none` at the same time or with
|
2022-01-07 15:16:40 +00:00
|
|
|
// other operation_ values, so we manually check for them and send an abbreviated object
|
2019-06-25 20:57:50 +00:00
|
|
|
let json = snapshot.serialize();
|
2019-07-02 21:23:07 +00:00
|
|
|
let keys = snapshot.record.nonOperationFields.map(decamelize);
|
|
|
|
let nonOperationFields = getProperties(json, keys);
|
|
|
|
for (let field in nonOperationFields) {
|
|
|
|
if (nonOperationFields[field] == null) {
|
|
|
|
delete nonOperationFields[field];
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 20:57:50 +00:00
|
|
|
if (json.operation_all) {
|
2019-07-02 21:23:07 +00:00
|
|
|
return {
|
|
|
|
operation_all: true,
|
|
|
|
...nonOperationFields,
|
|
|
|
};
|
2019-06-25 20:57:50 +00:00
|
|
|
}
|
|
|
|
if (json.operation_none) {
|
2019-07-02 21:23:07 +00:00
|
|
|
return {
|
|
|
|
operation_none: true,
|
|
|
|
...nonOperationFields,
|
|
|
|
};
|
2019-06-25 20:57:50 +00:00
|
|
|
}
|
|
|
|
delete json.operation_none;
|
|
|
|
delete json.operation_all;
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
updateRecord() {
|
|
|
|
return this.createRecord(...arguments);
|
|
|
|
},
|
|
|
|
});
|