source: Dev/trunk/src/client/qed-client/model/classes/responses.js @ 487

Last change on this file since 487 was 487, checked in by hendrikvanantwerpen, 11 years ago

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File size: 3.3 KB
Line 
1define([
2    "./_Class",
3    "./surveyRuns",
4    "dojo/Deferred",
5    "dojo/_base/declare",
6    "dojo/_base/json",
7    "dojo/_base/lang",
8    "dojo/_base/xhr",
9    "dojo/date/stamp"
10], function(_Class, surveyRuns, Deferred, declare, json, lang, xhr, stamp) {
11
12    var Responses = declare([_Class],{
13        _collection: 'responses',
14        _type: 'Response',
15        create: function() {
16            var obj = {
17                type: this._type,
18                answers: {},
19                surveyRunId: null
20            };
21            return obj;
22        },
23        _deserialize: function(obj) {
24            if (obj._surveyRun) {
25                obj._surveyRun = surveyRuns._doDeserialize(obj._surveyRun);
26            }
27            if (obj.publicationDate) {
28                obj.publicationDate = stamp.fromISOString(obj.publicationDate);
29            }
30        },
31        _serialize: function(obj) {
32            if (obj._surveyRun) {
33                obj._surveyRun = surveyRuns._doSerialize(obj._surveyRun);
34            }
35            if (obj.publicationDate) {
36                obj.publicationDate = stamp.toISOString(obj.publicationDate);
37            }
38        },
39        getWithSecret: function(id,secret) {
40            var query = xhr.objectToQuery({secret:secret});
41            return xhr('GET',{
42                url: '/api/open/responses/' + id + '?' + query,
43                handleAs: 'json',
44                contentType: false
45            }).then(lang.hitch(this,'_doDeserialize'),function(err){
46                return new Deferred().reject(json.fromJson(err.responseText));
47            });
48        },
49        postWithSecret: function(response,secret) {
50            var query = xhr.objectToQuery({secret:secret});
51            var body = json.toJson(this._doSerialize(response));
52            return xhr('POST',{
53                url: '/api/open/responses?' + query,
54                handleAs: 'json',
55                contentType: 'application/json',
56                rawBody: body
57            }).then(lang.hitch(this,'_doDeserialize'),function(err){
58                return new Deferred().reject(json.fromJson(err.responseText));
59            });
60        },
61        putWithSecret: function(response,secret) {
62            var query = xhr.objectToQuery({secret:secret});
63            var body = json.toJson(this._doSerialize(response));
64            return xhr('PUT',{
65                url: '/api/open/responses/' + this.getId(response) + '?' + query,
66                handleAs: 'json',
67                contentType: 'application/json',
68                rawBody: body
69            }).then(lang.hitch(this,'_doDeserialize'),function(err){
70                return new Deferred().reject(json.fromJson(err.responseText));
71            });
72        },
73        removeWithSecret: function(response,secret) {
74            var query = xhr.objectToQuery({secret:secret});
75            var rev = this.getRev(response);
76            var body = json.toJson(this._doSerialize(response));
77            var headers = {};
78            if ( rev ) {
79                headers['If-Match'] = '"'+rev+'"';
80            }
81            return xhr('DELETE',{
82                url: '/api/open/responses/' + this.getId(response) + '?' + query,
83                headers: headers,
84                handleAs: 'json',
85                contentType: 'application/json',
86                rawBody: body
87            });
88        }
89    });
90
91    return new Responses();
92
93});
Note: See TracBrowser for help on using the repository browser.