]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/store/util/QueryResults.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dojo / store / util / QueryResults.js
CommitLineData
81bea17a
AD
1/*
2 Copyright (c) 2004-2011, 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
7
8if(!dojo._hasResource["dojo.store.util.QueryResults"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dojo.store.util.QueryResults"] = true;
10dojo.provide("dojo.store.util.QueryResults");
11
12dojo.getObject("store.util", true, dojo);
13
14dojo.store.util.QueryResults = function(results){
15 // summary:
16 // A function that wraps the results of a store query with additional
17 // methods.
18 //
19 // description:
20 // QueryResults is a basic wrapper that allows for array-like iteration
21 // over any kind of returned data from a query. While the simplest store
22 // will return a plain array of data, other stores may return deferreds or
23 // promises; this wrapper makes sure that *all* results can be treated
24 // the same.
25 //
26 // Additional methods include `forEach`, `filter` and `map`.
27 //
28 // returns: Object
29 // An array-like object that can be used for iterating over.
30 //
31 // example:
32 // Query a store and iterate over the results.
33 //
34 // | store.query({ prime: true }).forEach(function(item){
35 // | // do something
36 // | });
37
38 if(!results){
39 return results;
40 }
41 // if it is a promise it may be frozen
42 if(results.then){
43 results = dojo.delegate(results);
44 }
45 function addIterativeMethod(method){
46 if(!results[method]){
47 results[method] = function(){
48 var args = arguments;
49 return dojo.when(results, function(results){
50 Array.prototype.unshift.call(args, results);
51 return dojo.store.util.QueryResults(dojo[method].apply(dojo, args));
52 });
53 };
54 }
55 }
56 addIterativeMethod("forEach");
57 addIterativeMethod("filter");
58 addIterativeMethod("map");
59 if(!results.total){
60 results.total = dojo.when(results, function(results){
61 return results.length;
62 });
63 }
64 return results;
65};
66
67}