source: Dev/branches/rest-dojo-ui/client/dojox/data/util/JsonQuery.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.7 KB
Line 
1define("dojox/data/util/JsonQuery", ["dojo", "dojox"], function(dojo, dojox) {
2
3// this is a mixin to convert object attribute queries to
4// JSONQuery/JSONPath syntax to be sent to the server.
5dojo.declare("dojox.data.util.JsonQuery", null, {
6        useFullIdInQueries: false,
7        _toJsonQuery: function(args, jsonQueryPagination){
8                var first = true;
9                var self = this;
10                function buildQuery(path, query){
11                        var isDataItem = query.__id;
12                        if(isDataItem){
13                                // it is a reference to a persisted object, need to make it a query by id
14                                var newQuery = {};
15                                newQuery[self.idAttribute] = self.useFullIdInQueries ? query.__id : query[self.idAttribute];
16                                query = newQuery;
17                        }
18                        for(var i in query){
19                                // iterate through each property, adding them to the overall query
20                                var value = query[i];
21                                var newPath = path + (/^[a-zA-Z_][\w_]*$/.test(i) ? '.' + i : '[' + dojo._escapeString(i) + ']');
22                                if(value && typeof value == "object"){
23                                        buildQuery(newPath, value);
24                                }else if(value!="*"){ // full wildcards can be ommitted
25                                        jsonQuery += (first ? "" : "&") + newPath +
26                                                ((!isDataItem && typeof value == "string" && args.queryOptions && args.queryOptions.ignoreCase) ? "~" : "=") +
27                                                 (self.simplifiedQuery ? encodeURIComponent(value) : dojo.toJson(value));
28                                        first = false;
29                                }
30                        }
31                }
32                // performs conversion of Dojo Data query objects and sort arrays to JSONQuery strings
33                if(args.query && typeof args.query == "object"){
34                        // convert Dojo Data query objects to JSONQuery
35                        var jsonQuery = "[?(";
36                        buildQuery("@", args.query);
37                        if(!first){
38                                // use ' instead of " for quoting in JSONQuery, and end with ]
39                                jsonQuery += ")]";
40                        }else{
41                                jsonQuery = "";
42                        }
43                        args.queryStr = jsonQuery.replace(/\\"|"/g,function(t){return t == '"' ? "'" : t;});
44                }else if(!args.query || args.query == '*'){
45                        args.query = "";
46                }
47               
48                var sort = args.sort;
49                if(sort){
50                        // if we have a sort order, add that to the JSONQuery expression
51                        args.queryStr = args.queryStr || (typeof args.query == 'string' ? args.query : "");
52                        first = true;
53                        for(i = 0; i < sort.length; i++){
54                                args.queryStr += (first ? '[' : ',') + (sort[i].descending ? '\\' : '/') + "@[" + dojo._escapeString(sort[i].attribute) + "]";
55                                first = false;
56                        }
57                        args.queryStr += ']';
58                }
59                // this is optional because with client side paging JSONQuery doesn't yield the total count
60                if(jsonQueryPagination && (args.start || args.count)){
61                        // pagination
62                        args.queryStr = (args.queryStr || (typeof args.query == 'string' ? args.query : "")) +
63                                '[' + (args.start || '') + ':' + (args.count ? (args.start || 0) + args.count : '') + ']';
64                }
65                if(typeof args.queryStr == 'string'){
66                        args.queryStr = args.queryStr.replace(/\\"|"/g,function(t){return t == '"' ? "'" : t;});
67                        return args.queryStr;
68                }
69                return args.query;
70        },
71        jsonQueryPagination: true,
72        fetch: function(args){
73                this._toJsonQuery(args, this.jsonQueryPagination);
74                return this.inherited(arguments);
75        },
76        isUpdateable: function(){
77                return true;
78        },
79        matchesQuery: function(item,request){
80                request._jsonQuery = request._jsonQuery || dojox.json.query(this._toJsonQuery(request));
81                return request._jsonQuery([item]).length;
82        },
83        clientSideFetch: function(/*Object*/ request,/*Array*/ baseResults){
84                request._jsonQuery = request._jsonQuery || dojox.json.query(this._toJsonQuery(request));
85                // we use client side paging function here instead of JSON Query because we must also determine the total count
86                return this.clientSidePaging(request, request._jsonQuery(baseResults));
87        },
88        querySuperSet: function(argsSuper,argsSub){
89                if(!argsSuper.query){
90                        return argsSub.query;
91                }
92                return this.inherited(arguments);
93        }
94       
95});
96
97return dojox.data.util.JsonQuery;
98});
Note: See TracBrowser for help on using the repository browser.