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

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

Application header is link to menu now.
Links in menu page are clickable now.
Added some logic to session page. Simple props can now be edited and saved.
Different actions for session templates and instances in sessions page.
Cleaner code in ObjectBox?. No special cases anymore, scope error fixed.
Fixed scope error in store.js.

File size: 5.8 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                    queryOpts.include_docs = true;
100                }
101
102                if (!lang.isString(query)) {
103                    console.warn("Query must be a view name");
104                }
105
106                // Standard options
107                if (options.start >= 0) {
108                    queryOpts.skip = options.start;
109                }
110                if (options.count >= 0) {
111                    queryOpts.limit = options.count;
112                }
113                if (options.sort) {
114                    if (options.sort[0]) {
115                        if (options.sort[0].attribute && options.sort[0].attribute !== "key") {
116                            console.warn("Can only sort on key");
117                        }
118                        if (options.sort[0].descending) {
119                            queryOpts.descending = true;
120                        }
121                    }
122                    if (options.sort.length > 1) {
123                        console.warn("multiple sort fields not supported");
124                    }
125                }
126
127                // Custom options
128                if (options.key) {
129                    queryOpts.key = options.key;
130                } else if (options.keys) {
131                    queryOpts.keys = options.keys;
132                } else if (options.startkey || options.endkey) {
133                    queryOpts.startkey = options.startkey;
134                    queryOpts.endkey = options.endkey;
135                }
136
137                for (var qp in queryOpts) {
138                    queryOpts[qp] = JSON.stringify(queryOpts[qp]);
139                }
140                            query += '?' + xhr.objectToQuery(queryOpts);
141
142                        xhr("GET", {
143                                url: this.target + query,
144                                handleAs: "json",
145                                headers: {
146                        Accept: this.accepts
147                    }
148                        }).then(function(result){
149                    if (result.error) {
150                        dfd.reject(result.reason);
151                    } else  {
152                        dfd.resolve(
153                            array.map(result.rows,
154                                      function(result){ return result.value; }));
155                    }
156                },function(err){
157                    dfd.reject(err);
158                });
159                        return QueryResults(dfd.promise);
160            }
161        });
162
163        return new CouchStore({target: 'data/rft/'});
164
165    });
Note: See TracBrowser for help on using the repository browser.