JsonQuery.js 3.7 KB

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