source: Dev/branches/rest-dojo-ui/client/rft/store.js @ 362

Last change on this file since 362 was 362, checked in by hendrikvanantwerpen, 13 years ago

Selector can be clicked on whole line now.
Question editor in survey page does lazy loading per
category and displays total number of questions.

File size: 7.1 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/_base/xhr','dojo/json','dojo/store/util/QueryResults'],
2    function(declare,lang,array,Deferred,xhr,JSON,QueryResults){
3   
4        var CouchStore = declare("rft.store", null, {
5            /** dojo.Store implementation for CouchDB
6             *
7             * See for details on the REST API, the wiki
8             * at http://wiki.apache.org/couchdb/HTTP_Document_API.
9             */
10                target: "",
11                accepts: "application/json",
12            idProperty: "_id",
13            revProperty: "_rev",
14            _responseIdProperty: "id",
15            _responseRevProperty: "rev",
16                constructor: function(options){
17                        declare.safeMixin(this, options);
18            },
19                getIdentity: function(object){
20                        return object[this.idProperty];
21            },
22                getRevision: function(object){
23                        return object[this.revProperty];
24            },
25                get: function(id){
26                var dfd = new Deferred();
27                        xhr("GET", {
28                                url: this.target + id,
29                                handleAs: "json",
30                                headers: {
31                                Accept: this.accepts
32                    }
33                        }).then(function(result){
34                    if ( result.error ) {
35                        dfd.reject(result.reason);
36                    } else {
37                        dfd.resolve(result);
38                    }
39                }, function(err){
40                    dfd.reject(err);
41                });
42                return dfd.promise;
43            },
44                put: function(object, options){
45                        options = options || {};
46
47                var dfd = new Deferred();
48                        var id = options.id ? options.id : this.getIdentity(object);
49                        var hasId = typeof id != "undefined";
50                        xhr(hasId ? "PUT" : "POST", {
51                                    url: hasId ? this.target + id : this.target,
52                                    postData: JSON.stringify(object),
53                                    handleAs: "json",
54                                    headers:{
55                                            "Content-Type": "application/json",
56                                            Accept: this.accepts
57                                    }
58                            }).then(lang.hitch(this,function(result){
59                    if ( result.error ) {
60                        dfd.reject(result.reason);
61                    } else {
62                        object[this.idProperty] = result[this._responseIdProperty];
63                        object[this.revProperty] = result[this._responseRevProperty];
64                        dfd.resolve(object);
65                    }
66                }), function(err){
67                    dfd.reject(err);
68                });
69                return dfd.promise;
70            },
71                add: function(object, options){
72                return this.put(object,options);
73            },
74                remove: function(id,rev){
75                var dfd = new Deferred();
76                        xhr("DELETE",{
77                                url: this.target + id,
78                                    headers: {
79                        'If-Match': rev
80                    }
81                        }).then(function(result){
82                    if ( result.error ) {
83                        dfd.reject(result.reason);
84                    } else {
85                        dfd.resolve();
86                    }
87                },function(err){
88                    dfd.reject(err);
89                });
90                return dfd.promise;
91            },
92            query: function(query, options){
93                        options = options || {};
94
95                var dfd = new Deferred();
96                var queryOpts = {};
97                if ( query === undefined ) {
98                    query = '_all_docs';
99                }
100
101                if (!lang.isString(query)) {
102                    console.warn("Query must be a view name");
103                }
104
105                // Standard options
106                if (options.start >= 0) {
107                    queryOpts.skip = options.start;
108                }
109                if (options.count >= 0) {
110                    queryOpts.limit = options.count;
111                }
112                if (options.sort) {
113                    if (options.sort[0]) {
114                        if (options.sort[0].attribute && options.sort[0].attribute !== "key") {
115                            console.warn("Can only sort on key");
116                        }
117                        if (options.sort[0].descending) {
118                            queryOpts.descending = true;
119                        }
120                    }
121                    if (options.sort.length > 1) {
122                        console.warn("multiple sort fields not supported");
123                    }
124                }
125
126                // Custom options
127                if (options.key !== undefined) {
128                    queryOpts.key = options.key;
129                } else if (options.keys !== undefined) {
130                    queryOpts.keys = options.keys;
131                } else if (options.startkey !== undefined || options.endkey !== undefined) {
132                    queryOpts.startkey = options.startkey;
133                    queryOpts.endkey = options.endkey;
134                }
135                if (options.include_docs !== undefined) {
136                    queryOpts.include_docs = options.include_docs;
137                }
138                if (options.reduce !== undefined) {
139                    queryOpts.reduce = options.reduce;
140                }
141                if (options.group !== undefined) {
142                    queryOpts.group = options.group;
143                    if (options.group_level !== undefined) {
144                        queryOpts.group_level = options.group_level;
145                    }
146                }
147
148                for (var qp in queryOpts) {
149                    queryOpts[qp] = JSON.stringify(queryOpts[qp]);
150                }
151                            query += '?' + xhr.objectToQuery(queryOpts);
152
153                        xhr("GET", {
154                                url: this.target + query,
155                                handleAs: "json",
156                                headers: {
157                        Accept: this.accepts
158                    }
159                        }).then(function(result){
160                    if (result.error) {
161                        dfd.reject(result.reason);
162                    } else  {
163                        dfd.resolve(
164                            array.map(result.rows,
165                                      function(result){
166                                          return [result.key, options.include_docs === true ? result.doc : result.value];
167                                      }));
168                    }
169                },function(err){
170                    dfd.reject(err);
171                });
172                        return CouchResults(dfd.promise);
173            }
174        });
175
176        function CouchResults(results) {
177            if (!results) {
178                return results;
179            }
180
181                if(results.then){
182                        results = lang.delegate(results);
183                }
184
185            if (!results.forEach) {
186                results.forEach = function(callback) {
187                    return Deferred.when(results, function(results) {
188                        array.forEach(results, function(result) {
189                            callback(result[1],result[0]);
190                        });
191                    });
192                }
193            }
194
195            return results;
196        }
197
198        return new CouchStore({target: 'data/rft/'});
199
200    });
Note: See TracBrowser for help on using the repository browser.