-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelasticsearch.angular.simple.js
42 lines (38 loc) · 1.43 KB
/
elasticsearch.angular.simple.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
angular.module('elasticsearch', [])
.factory('esFactory', ['$http', '$q', function ($http, $q) {
return function (clientOpts) {
return {
search: function (searchOpts) {
var deferredAbort = $q.defer();
var defer = $q.defer();
$http({
method: 'POST',
url: clientOpts.host + "/" + searchOpts.index + '/_search',
params: {
size: searchOpts.size,
from: searchOpts.from
},
data: searchOpts.body.toJSON(),
timeout: deferredAbort.promise
}).then(
function (body) {
defer.resolve(body.data);
},
function () {
defer.reject.apply(this, arguments);
}
);
defer.promise.abort = function () {
deferredAbort.resolve();
};
// cleanup (http://stackoverflow.com/questions/24440177/angularjs-how-to-cancel-resource-promise-when-switching-routes)
defer.promise.finally(function() {
defer.promise.abort = angular.noop;
deferredAbort = defer = null;
}
);
return defer.promise;
}
};
};
}]);