source: Dev/trunk/src/qed-client/store/ElasticReadStore.js @ 446

Last change on this file since 446 was 443, checked in by hendrikvanantwerpen, 12 years ago

Reorganized for Node --- the SVN gods hate us all!

Lost all historical info on moved files, because SVN is a f *.

Also we have Node now, serving both the static content and forwarding
database requests.

File size: 1.8 KB
Line 
1define([
2    'dojo/_base/declare',
3    "dojo/_base/json",
4    'dojo/_base/lang',
5    'dojo/_base/xhr',
6    'dojox/data/QueryReadStore'
7],function(declare, json, lang, xhr, QueryReadStore) {
8    return declare(QueryReadStore, {
9        fetch:function(request){
10            var attr = Object.keys(request.query)[0];
11            if (request.query[attr].length === 0) {
12                return 0;
13            }
14            var q = request.query[attr];
15
16            request.serverQuery = json.toJson({
17                query: {
18                    query_string: {
19                        default_field: attr,
20                        query: q
21                    }
22                }
23            });
24            return this.inherited(arguments);
25        },
26        _fetchItems: function(request, fetchHandler, errorHandler){
27            var serverQuery = request.serverQuery;
28            var xhrHandler = xhr.post({
29                url: this.url,
30                handleAs: "json",
31                postData: serverQuery
32            }).then(lang.hitch(this, function(data){
33                this._xhrFetchHandler(data, request, fetchHandler, errorHandler);
34            }),function(error){
35                errorHandler(error, request);
36            });
37            request.abort = function(){
38                xhrHandler.cancel();
39            };
40        },
41        _xhrFetchHandler: function(data, request, fetchHandler, errorHandler) {
42            data = this._filterResponse(data);
43            this._items = [];
44            var numHits = data.hits.total || -1;
45            if(numHits > 0) {
46                this._items.push({i:data.hits.hits[0]._source, r:this, n:0});
47            }
48            fetchHandler(this._items, request, data.hits.total);
49            this._numRows = data.hits.total;
50        }
51    });
52});
Note: See TracBrowser for help on using the repository browser.