diff options
Diffstat (limited to 'lib/dojo/store/util')
-rw-r--r-- | lib/dojo/store/util/QueryResults.js | 63 | ||||
-rw-r--r-- | lib/dojo/store/util/QueryResults.js.uncompressed.js | 64 | ||||
-rw-r--r-- | lib/dojo/store/util/SimpleQueryEngine.js | 112 | ||||
-rw-r--r-- | lib/dojo/store/util/SimpleQueryEngine.js.uncompressed.js | 108 |
4 files changed, 176 insertions, 171 deletions
diff --git a/lib/dojo/store/util/QueryResults.js b/lib/dojo/store/util/QueryResults.js index a237d1dce..fc164ec6b 100644 --- a/lib/dojo/store/util/QueryResults.js +++ b/lib/dojo/store/util/QueryResults.js @@ -4,64 +4,5 @@ see: http://dojotoolkit.org/license for details */ - -if(!dojo._hasResource["dojo.store.util.QueryResults"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. -dojo._hasResource["dojo.store.util.QueryResults"] = true; -dojo.provide("dojo.store.util.QueryResults"); - -dojo.getObject("store.util", true, dojo); - -dojo.store.util.QueryResults = function(results){ - // summary: - // A function that wraps the results of a store query with additional - // methods. - // - // description: - // QueryResults is a basic wrapper that allows for array-like iteration - // over any kind of returned data from a query. While the simplest store - // will return a plain array of data, other stores may return deferreds or - // promises; this wrapper makes sure that *all* results can be treated - // the same. - // - // Additional methods include `forEach`, `filter` and `map`. - // - // returns: Object - // An array-like object that can be used for iterating over. - // - // example: - // Query a store and iterate over the results. - // - // | store.query({ prime: true }).forEach(function(item){ - // | // do something - // | }); - - if(!results){ - return results; - } - // if it is a promise it may be frozen - if(results.then){ - results = dojo.delegate(results); - } - function addIterativeMethod(method){ - if(!results[method]){ - results[method] = function(){ - var args = arguments; - return dojo.when(results, function(results){ - Array.prototype.unshift.call(args, results); - return dojo.store.util.QueryResults(dojo[method].apply(dojo, args)); - }); - }; - } - } - addIterativeMethod("forEach"); - addIterativeMethod("filter"); - addIterativeMethod("map"); - if(!results.total){ - results.total = dojo.when(results, function(results){ - return results.length; - }); - } - return results; -}; - -} +//>>built +define("dojo/store/util/QueryResults",["../../_base/array","../../_base/lang","../../_base/Deferred"],function(_1,_2,_3){var _4=_2.getObject("dojo.store.util",true);_4.QueryResults=function(_5){if(!_5){return _5;}if(_5.then){_5=_2.delegate(_5);}function _6(_7){if(!_5[_7]){_5[_7]=function(){var _8=arguments;return _3.when(_5,function(_9){Array.prototype.unshift.call(_8,_9);return _4.QueryResults(_1[_7].apply(_1,_8));});};}};_6("forEach");_6("filter");_6("map");if(!_5.total){_5.total=_3.when(_5,function(_a){return _a.length;});}return _5;};return _4.QueryResults;});
\ No newline at end of file diff --git a/lib/dojo/store/util/QueryResults.js.uncompressed.js b/lib/dojo/store/util/QueryResults.js.uncompressed.js new file mode 100644 index 000000000..cd9e1969a --- /dev/null +++ b/lib/dojo/store/util/QueryResults.js.uncompressed.js @@ -0,0 +1,64 @@ +define("dojo/store/util/QueryResults", ["../../_base/array", "../../_base/lang", "../../_base/Deferred" +], function(array, lang, Deferred) { + // module: + // dojo/store/util/QueryResults + // summary: + // The module defines a query results wrapper + +var util = lang.getObject("dojo.store.util", true); + +util.QueryResults = function(results){ + // summary: + // A function that wraps the results of a store query with additional + // methods. + // + // description: + // QueryResults is a basic wrapper that allows for array-like iteration + // over any kind of returned data from a query. While the simplest store + // will return a plain array of data, other stores may return deferreds or + // promises; this wrapper makes sure that *all* results can be treated + // the same. + // + // Additional methods include `forEach`, `filter` and `map`. + // + // returns: Object + // An array-like object that can be used for iterating over. + // + // example: + // Query a store and iterate over the results. + // + // | store.query({ prime: true }).forEach(function(item){ + // | // do something + // | }); + + if(!results){ + return results; + } + // if it is a promise it may be frozen + if(results.then){ + results = lang.delegate(results); + } + function addIterativeMethod(method){ + if(!results[method]){ + results[method] = function(){ + var args = arguments; + return Deferred.when(results, function(results){ + Array.prototype.unshift.call(args, results); + return util.QueryResults(array[method].apply(array, args)); + }); + }; + } + } + addIterativeMethod("forEach"); + addIterativeMethod("filter"); + addIterativeMethod("map"); + if(!results.total){ + results.total = Deferred.when(results, function(results){ + return results.length; + }); + } + return results; +}; + +return util.QueryResults; +}); diff --git a/lib/dojo/store/util/SimpleQueryEngine.js b/lib/dojo/store/util/SimpleQueryEngine.js index 4b29f20cb..aa398f6e7 100644 --- a/lib/dojo/store/util/SimpleQueryEngine.js +++ b/lib/dojo/store/util/SimpleQueryEngine.js @@ -4,113 +4,5 @@ see: http://dojotoolkit.org/license for details */ - -if(!dojo._hasResource["dojo.store.util.SimpleQueryEngine"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. -dojo._hasResource["dojo.store.util.SimpleQueryEngine"] = true; -dojo.provide("dojo.store.util.SimpleQueryEngine"); - -dojo.getObject("store.util", true, dojo); - -dojo.store.util.SimpleQueryEngine = function(query, options){ - // summary: - // Simple query engine that matches using filter functions, named filter - // functions or objects by name-value on a query object hash - // - // description: - // The SimpleQueryEngine provides a way of getting a QueryResults through - // the use of a simple object hash as a filter. The hash will be used to - // match properties on data objects with the corresponding value given. In - // other words, only exact matches will be returned. - // - // This function can be used as a template for more complex query engines; - // for example, an engine can be created that accepts an object hash that - // contains filtering functions, or a string that gets evaluated, etc. - // - // When creating a new dojo.store, simply set the store's queryEngine - // field as a reference to this function. - // - // query: Object - // An object hash with fields that may match fields of items in the store. - // Values in the hash will be compared by normal == operator, but regular expressions - // or any object that provides a test() method are also supported and can be - // used to match strings by more complex expressions - // (and then the regex's or object's test() method will be used to match values). - // - // options: dojo.store.util.SimpleQueryEngine.__queryOptions? - // An object that contains optional information such as sort, start, and count. - // - // returns: Function - // A function that caches the passed query under the field "matches". See any - // of the "query" methods on dojo.stores. - // - // example: - // Define a store with a reference to this engine, and set up a query method. - // - // | var myStore = function(options){ - // | // ...more properties here - // | this.queryEngine = dojo.store.util.SimpleQueryEngine; - // | // define our query method - // | this.query = function(query, options){ - // | return dojo.store.util.QueryResults(this.queryEngine(query, options)(this.data)); - // | }; - // | }; - - // create our matching query function - switch(typeof query){ - default: - throw new Error("Can not query with a " + typeof query); - case "object": case "undefined": - var queryObject = query; - query = function(object){ - for(var key in queryObject){ - var required = queryObject[key]; - if(required && required.test){ - if(!required.test(object[key])){ - return false; - } - }else if(required != object[key]){ - return false; - } - } - return true; - }; - break; - case "string": - // named query - if(!this[query]){ - throw new Error("No filter function " + query + " was found in store"); - } - query = this[query]; - // fall through - case "function": - // fall through - } - function execute(array){ - // execute the whole query, first we filter - var results = dojo.filter(array, query); - // next we sort - if(options && options.sort){ - results.sort(function(a, b){ - for(var sort, i=0; sort = options.sort[i]; i++){ - var aValue = a[sort.attribute]; - var bValue = b[sort.attribute]; - if (aValue != bValue) { - return !!sort.descending == aValue > bValue ? -1 : 1; - } - } - return 0; - }); - } - // now we paginate - if(options && (options.start || options.count)){ - var total = results.length; - results = results.slice(options.start || 0, (options.start || 0) + (options.count || Infinity)); - results.total = total; - } - return results; - } - execute.matches = query; - return execute; -}; - -} +//>>built +define("dojo/store/util/SimpleQueryEngine",["../../_base/array"],function(_1){return function(_2,_3){switch(typeof _2){default:throw new Error("Can not query with a "+typeof _2);case "object":case "undefined":var _4=_2;_2=function(_5){for(var _6 in _4){var _7=_4[_6];if(_7&&_7.test){if(!_7.test(_5[_6])){return false;}}else{if(_7!=_5[_6]){return false;}}}return true;};break;case "string":if(!this[_2]){throw new Error("No filter function "+_2+" was found in store");}_2=this[_2];case "function":}function _8(_9){var _a=_1.filter(_9,_2);if(_3&&_3.sort){_a.sort(function(a,b){for(var _b,i=0;_b=_3.sort[i];i++){var _c=a[_b.attribute];var _d=b[_b.attribute];if(_c!=_d){return !!_b.descending==_c>_d?-1:1;}}return 0;});}if(_3&&(_3.start||_3.count)){var _e=_a.length;_a=_a.slice(_3.start||0,(_3.start||0)+(_3.count||Infinity));_a.total=_e;}return _a;};_8.matches=_2;return _8;};});
\ No newline at end of file diff --git a/lib/dojo/store/util/SimpleQueryEngine.js.uncompressed.js b/lib/dojo/store/util/SimpleQueryEngine.js.uncompressed.js new file mode 100644 index 000000000..4f241e6f1 --- /dev/null +++ b/lib/dojo/store/util/SimpleQueryEngine.js.uncompressed.js @@ -0,0 +1,108 @@ +define("dojo/store/util/SimpleQueryEngine", ["../../_base/array"], function(arrayUtil) { + // module: + // dojo/store/util/SimpleQueryEngine + // summary: + // The module defines a simple filtering query engine for object stores. + +return function(query, options){ + // summary: + // Simple query engine that matches using filter functions, named filter + // functions or objects by name-value on a query object hash + // + // description: + // The SimpleQueryEngine provides a way of getting a QueryResults through + // the use of a simple object hash as a filter. The hash will be used to + // match properties on data objects with the corresponding value given. In + // other words, only exact matches will be returned. + // + // This function can be used as a template for more complex query engines; + // for example, an engine can be created that accepts an object hash that + // contains filtering functions, or a string that gets evaluated, etc. + // + // When creating a new dojo.store, simply set the store's queryEngine + // field as a reference to this function. + // + // query: Object + // An object hash with fields that may match fields of items in the store. + // Values in the hash will be compared by normal == operator, but regular expressions + // or any object that provides a test() method are also supported and can be + // used to match strings by more complex expressions + // (and then the regex's or object's test() method will be used to match values). + // + // options: dojo.store.util.SimpleQueryEngine.__queryOptions? + // An object that contains optional information such as sort, start, and count. + // + // returns: Function + // A function that caches the passed query under the field "matches". See any + // of the "query" methods on dojo.stores. + // + // example: + // Define a store with a reference to this engine, and set up a query method. + // + // | var myStore = function(options){ + // | // ...more properties here + // | this.queryEngine = dojo.store.util.SimpleQueryEngine; + // | // define our query method + // | this.query = function(query, options){ + // | return dojo.store.util.QueryResults(this.queryEngine(query, options)(this.data)); + // | }; + // | }; + + // create our matching query function + switch(typeof query){ + default: + throw new Error("Can not query with a " + typeof query); + case "object": case "undefined": + var queryObject = query; + query = function(object){ + for(var key in queryObject){ + var required = queryObject[key]; + if(required && required.test){ + if(!required.test(object[key])){ + return false; + } + }else if(required != object[key]){ + return false; + } + } + return true; + }; + break; + case "string": + // named query + if(!this[query]){ + throw new Error("No filter function " + query + " was found in store"); + } + query = this[query]; + // fall through + case "function": + // fall through + } + function execute(array){ + // execute the whole query, first we filter + var results = arrayUtil.filter(array, query); + // next we sort + if(options && options.sort){ + results.sort(function(a, b){ + for(var sort, i=0; sort = options.sort[i]; i++){ + var aValue = a[sort.attribute]; + var bValue = b[sort.attribute]; + if (aValue != bValue) { + return !!sort.descending == aValue > bValue ? -1 : 1; + } + } + return 0; + }); + } + // now we paginate + if(options && (options.start || options.count)){ + var total = results.length; + results = results.slice(options.start || 0, (options.start || 0) + (options.count || Infinity)); + results.total = total; + } + return results; + } + execute.matches = query; + return execute; +}; +}); |