Skip to content

Instantly share code, notes, and snippets.

@greyby
Last active December 7, 2017 13:17
Show Gist options
  • Save greyby/c6ad2dd5f4592867d43d to your computer and use it in GitHub Desktop.
Save greyby/c6ad2dd5f4592867d43d to your computer and use it in GitHub Desktop.
custom ember data's restful URL and modify the HTTP method
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
// namespace: 'api/v2',
host: 'http://localhost:8080',
find: function(store, type, id, snapshot) {
return this.ajax(this.buildURL(type.typeKey, id, snapshot, 'find'), 'GET');
},
findAll: function(store, type, sinceToken) {
var query, url;
if (sinceToken) {
query = {
since: sinceToken
};
}
url = this.buildURL(type.typeKey, null, null, 'findAll');
return this.ajax(url + "/list", 'GET', {
data: query
});
},
createRecord: function(store, type, snapshot) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
var url = this.buildURL(type.typeKey, null, snapshot, 'createRecord');
serializer.serializeIntoHash(data, type, snapshot, {
includeId: true
});
return this.ajax(url + "/add", "POST", {
data: data
});
},
updateRecord: function(store, type, snapshot) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, snapshot);
var id = snapshot.id;
var url = this.buildURL(type.typeKey, '', snapshot, 'updateRecord');
return this.ajax(url + "/update/"+id, "POST", {
data: data
});
},
deleteRecord: function(store, type, snapshot) {
var id = snapshot.id;
return this.ajax(this.buildURL(type.typeKey, '', snapshot, 'deleteRecord') + "/delete/"+id, "POST");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment