1 | define(["dojo", "dojox", "dojo/json"], function(dojo, dojox, JSON) {
|
---|
2 | dojo.provide("rft.elastic.ElasticReadStore");
|
---|
3 |
|
---|
4 | dojo.require("dojox.data.QueryReadStore");
|
---|
5 |
|
---|
6 | dojo.declare("rft.elastic.ElasticReadStore", dojox.data.QueryReadStore, {
|
---|
7 | fetch:function(request){
|
---|
8 | request.serverQuery = dojo.toJson({
|
---|
9 | query:
|
---|
10 | {
|
---|
11 | query_string:
|
---|
12 | {
|
---|
13 | default_field: "title",
|
---|
14 | query: request.query.title
|
---|
15 | }
|
---|
16 | }
|
---|
17 | });
|
---|
18 | // Call superclasses' fetch
|
---|
19 | return this.inherited("fetch", arguments);
|
---|
20 | },
|
---|
21 |
|
---|
22 | fetch:function(/* Object? */ request){
|
---|
23 | // summary:
|
---|
24 | // See dojo.data.util.simpleFetch.fetch() this is just a copy and I adjusted
|
---|
25 | // only the paging, since it happens on the server if doClientPaging is
|
---|
26 | // false, thx to http://trac.dojotoolkit.org/ticket/4761 reporting this.
|
---|
27 | // Would be nice to be able to use simpleFetch() to reduce copied code,
|
---|
28 | // but i dont know how yet. Ideas please!
|
---|
29 | request = request || {};
|
---|
30 | if(!request.store){
|
---|
31 | request.store = this;
|
---|
32 | }
|
---|
33 | var self = this;
|
---|
34 |
|
---|
35 | var _errorHandler = function(errorData, requestObject){
|
---|
36 | if(requestObject.onError){
|
---|
37 | var scope = requestObject.scope || dojo.global;
|
---|
38 | requestObject.onError.call(scope, errorData, requestObject);
|
---|
39 | }
|
---|
40 | };
|
---|
41 |
|
---|
42 | var _fetchHandler = function(items, requestObject, numRows){
|
---|
43 | var oldAbortFunction = requestObject.abort || null;
|
---|
44 | var aborted = false;
|
---|
45 |
|
---|
46 | var startIndex = requestObject.start?requestObject.start:0;
|
---|
47 | if(self.doClientPaging == false){
|
---|
48 | // For client paging we dont need no slicing of the result.
|
---|
49 | startIndex = 0;
|
---|
50 | }
|
---|
51 | var endIndex = requestObject.count?(startIndex + requestObject.count):items.length;
|
---|
52 |
|
---|
53 | requestObject.abort = function(){
|
---|
54 | aborted = true;
|
---|
55 | if(oldAbortFunction){
|
---|
56 | oldAbortFunction.call(requestObject);
|
---|
57 | }
|
---|
58 | };
|
---|
59 |
|
---|
60 | var scope = requestObject.scope || dojo.global;
|
---|
61 | if(!requestObject.store){
|
---|
62 | requestObject.store = self;
|
---|
63 | }
|
---|
64 | if(requestObject.onBegin){
|
---|
65 | requestObject.onBegin.call(scope, numRows, requestObject);
|
---|
66 | }
|
---|
67 | if(requestObject.sort && self.doClientSorting){
|
---|
68 | items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self));
|
---|
69 | }
|
---|
70 | if(requestObject.onItem){
|
---|
71 | for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
|
---|
72 | var item = items[i];
|
---|
73 | if(!aborted){
|
---|
74 | requestObject.onItem.call(scope, item, requestObject);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | if(requestObject.onComplete && !aborted){
|
---|
79 | var subset = null;
|
---|
80 | if(!requestObject.onItem){
|
---|
81 | subset = items.slice(startIndex, endIndex);
|
---|
82 | }
|
---|
83 | requestObject.onComplete.call(scope, subset, requestObject);
|
---|
84 | }
|
---|
85 | };
|
---|
86 | this._fetchItems(request, _fetchHandler, _errorHandler);
|
---|
87 | return request; // Object
|
---|
88 | },
|
---|
89 | });
|
---|
90 | }) |
---|