SimpleQueryEngine.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.store.util.SimpleQueryEngine"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.store.util.SimpleQueryEngine"] = true;
  8. dojo.provide("dojo.store.util.SimpleQueryEngine");
  9. dojo.getObject("store.util", true, dojo);
  10. dojo.store.util.SimpleQueryEngine = function(query, options){
  11. // summary:
  12. // Simple query engine that matches using filter functions, named filter
  13. // functions or objects by name-value on a query object hash
  14. //
  15. // description:
  16. // The SimpleQueryEngine provides a way of getting a QueryResults through
  17. // the use of a simple object hash as a filter. The hash will be used to
  18. // match properties on data objects with the corresponding value given. In
  19. // other words, only exact matches will be returned.
  20. //
  21. // This function can be used as a template for more complex query engines;
  22. // for example, an engine can be created that accepts an object hash that
  23. // contains filtering functions, or a string that gets evaluated, etc.
  24. //
  25. // When creating a new dojo.store, simply set the store's queryEngine
  26. // field as a reference to this function.
  27. //
  28. // query: Object
  29. // An object hash with fields that may match fields of items in the store.
  30. // Values in the hash will be compared by normal == operator, but regular expressions
  31. // or any object that provides a test() method are also supported and can be
  32. // used to match strings by more complex expressions
  33. // (and then the regex's or object's test() method will be used to match values).
  34. //
  35. // options: dojo.store.util.SimpleQueryEngine.__queryOptions?
  36. // An object that contains optional information such as sort, start, and count.
  37. //
  38. // returns: Function
  39. // A function that caches the passed query under the field "matches". See any
  40. // of the "query" methods on dojo.stores.
  41. //
  42. // example:
  43. // Define a store with a reference to this engine, and set up a query method.
  44. //
  45. // | var myStore = function(options){
  46. // | // ...more properties here
  47. // | this.queryEngine = dojo.store.util.SimpleQueryEngine;
  48. // | // define our query method
  49. // | this.query = function(query, options){
  50. // | return dojo.store.util.QueryResults(this.queryEngine(query, options)(this.data));
  51. // | };
  52. // | };
  53. // create our matching query function
  54. switch(typeof query){
  55. default:
  56. throw new Error("Can not query with a " + typeof query);
  57. case "object": case "undefined":
  58. var queryObject = query;
  59. query = function(object){
  60. for(var key in queryObject){
  61. var required = queryObject[key];
  62. if(required && required.test){
  63. if(!required.test(object[key])){
  64. return false;
  65. }
  66. }else if(required != object[key]){
  67. return false;
  68. }
  69. }
  70. return true;
  71. };
  72. break;
  73. case "string":
  74. // named query
  75. if(!this[query]){
  76. throw new Error("No filter function " + query + " was found in store");
  77. }
  78. query = this[query];
  79. // fall through
  80. case "function":
  81. // fall through
  82. }
  83. function execute(array){
  84. // execute the whole query, first we filter
  85. var results = dojo.filter(array, query);
  86. // next we sort
  87. if(options && options.sort){
  88. results.sort(function(a, b){
  89. for(var sort, i=0; sort = options.sort[i]; i++){
  90. var aValue = a[sort.attribute];
  91. var bValue = b[sort.attribute];
  92. if (aValue != bValue) {
  93. return !!sort.descending == aValue > bValue ? -1 : 1;
  94. }
  95. }
  96. return 0;
  97. });
  98. }
  99. // now we paginate
  100. if(options && (options.start || options.count)){
  101. var total = results.length;
  102. results = results.slice(options.start || 0, (options.start || 0) + (options.count || Infinity));
  103. results.total = total;
  104. }
  105. return results;
  106. }
  107. execute.matches = query;
  108. return execute;
  109. };
  110. }