Skip to content

Instantly share code, notes, and snippets.

@gintsgints
Last active January 2, 2016 07:29
Show Gist options
  • Save gintsgints/8270260 to your computer and use it in GitHub Desktop.
Save gintsgints/8270260 to your computer and use it in GitHub Desktop.
DS.ElasticAdapter = DS.RESTAdapter.extend({
ajax: function(url, type, hash) {
var adapter = this;
if (type == 'GET') {
url = url + "/_search"
} else {
hash.data = hash.data['company'];
};
return new Ember.RSVP.Promise(function(resolve, reject) {
hash = adapter.ajaxOptions(url, type, hash);
hash.success = function(json) {
// for elastic _search remove metadata. (Probably should be analyzed)
if (type == 'GET') {
if (json["hits"]) {
var resultname = json.hits.hits[0]._type;
var newdata = $.map(json.hits.hits, function(e) {
var result = e._source;
result["id"]=e._id;
return result;
});
json = {};
json[resultname] = newdata;
} else {
var newdata = json._source;
var resultname = json._type;
newdata["id"] = json._id;
json = {};
json[resultname] = newdata;
};
}
// elastic specific code ends;
Ember.run(null, resolve, json);
};
hash.error = function(jqXHR, textStatus, errorThrown) {
Ember.run(null, reject, adapter.ajaxError(jqXHR));
};
Ember.$.ajax(hash);
}, "DS: RestAdapter#ajax " + type + " to " + url);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment