Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created July 4, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeart/b60136a7ead7f687ac670f8b8792650c to your computer and use it in GitHub Desktop.
Save lifeart/b60136a7ead7f687ac670f8b8792650c to your computer and use it in GitHub Desktop.
Ember.adapters
import DS from 'ember-data';
import Ember from 'ember';
const {get, A, isArray} = Ember;
export default DS.JSONAPIAdapter.extend({
host: 'http://mysite.com/data',
pathMap: {
'query:partner-video': 'wm2/get_videos',
'query:partner-payment': 'wm2/get_payments',
'findAll:partner-video': 'wm2/get_videos',
'updateRecord:partner-video': 'wm2/set_file_info',
'updateRecord:partner-channel': A(['wm2/channel_change','wm2/get_channels']),
'updateRecord:partner': A(['wm2/set_user_data','wm2/get_user_data','wm2/get_purses']),
'query:partner-channel': 'wm2/get_channels',
'query:video-category': 'wm2/cat_list',
'query:partner-ftp-server': 'wm2servers/ftp_listing',
'deleteRecord:partner-channel': 'wm2/channel_delete',
'queryRecord:partner': A(['wm2/get_user_data','wm2/get_purses']),
'findRecord:partner': A(['wm2/get_user_data','wm2/get_purses']),
'queryRecord:partner-ftp-auth': 'wm2servers/ftp_auth_data',
},
_patchConfig(apiPath,query) {
let apis = isArray(apiPath)?apiPath:A([apiPath]);
let legacyQuery = apis.map(path=>{
return [path,isArray(query)?query:[query]];
});
return {
dataType: 'json',
data: {
r: JSON.stringify(legacyQuery)
},
xhrFields: {
withCredentials: true
},
crossDomain: true
};
},
findAll(store, type, sinceToken, snapshotRecordArray) {
let apiPath = this.pathMap[`findAll:${type.modelName}`];
const query = this.buildQuery(snapshotRecordArray);
if (sinceToken) {
query.since = sinceToken;
}
const url = this.buildURL(type.modelName, null, snapshotRecordArray, 'findAll');
return this.ajax(url, 'POST', this._patchConfig(apiPath,query));
},
updateRecord(store, type, snapshot) {
let apiPath = this.pathMap[`updateRecord:${type.modelName}`];
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, snapshot);
var id = snapshot.id;
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(url, 'POST', this._patchConfig(apiPath,data));
},
deleteRecord(store, type, snapshot) {
let apiPath = this.pathMap[`deleteRecord:${type.modelName}`];
var id = snapshot.id;
var url = this.buildURL(type.modelName, null, null, 'deleteRecord');
return this.ajax(url, 'POST', this._patchConfig(apiPath,id));
},
findRecord(store, type, id, snapshot) {
let apiPath = this.pathMap[`findRecord:${type.modelName}`];
var id = snapshot.id;
var url = this.buildURL(type.modelName, null, null, 'findRecord');
return this.ajax(url, 'POST', this._patchConfig(apiPath,id));
},
queryRecord(store, type, query) {
let apiPath = this.pathMap[`queryRecord:${type.modelName}`];
var url = this.buildURL(type.modelName, null, null, 'queryRecord', query);
return this.ajax(url, 'POST', this._patchConfig(apiPath,query));
},
query(store, type, query) {
let apiPath = this.pathMap[`query:${type.modelName}`];
let url = this.buildURL(type.modelName, null, null, 'query', query);
return this.ajax(url, 'POST', this._patchConfig(apiPath,query));
},
_buildURL() {
return this.get('host');
},
ajaxOptions (url, type, options) {
// console.log('ajaxOptions');
var hash = options || {};
hash.url = url;
hash.type = type;
hash.dataType = 'json';
hash.context = this;
var headers = get(this, 'headers');
if (headers !== undefined) {
hash.beforeSend = function (xhr) {
Object.keys(headers).forEach(function (key) {
return xhr.setRequestHeader(key, headers[key]);
});
};
}
return hash;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment