summaryrefslogtreecommitdiff
path: root/lib/dojo/store/util/QueryResults.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dojo/store/util/QueryResults.js')
-rw-r--r--lib/dojo/store/util/QueryResults.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/dojo/store/util/QueryResults.js b/lib/dojo/store/util/QueryResults.js
new file mode 100644
index 000000000..a237d1dce
--- /dev/null
+++ b/lib/dojo/store/util/QueryResults.js
@@ -0,0 +1,67 @@
+/*
+ Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
+ Available via Academic Free License >= 2.1 OR the modified BSD license.
+ 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;
+};
+
+}