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

Last change on this file was 510, checked in by hendrikvanantwerpen, 11 years ago
  • Factored out general object mapping and iteration.
  • Split widgets for multiplechoice and singlechoice.
  • Restored readOnly/disabled setting for QuestionEditorPreviewItem? on innerWidget (since view innerWidget is not a form anymore, we cannot just set it on that, we iterate over all form children now).
File size: 3.9 KB
RevLine 
[487]1define([
[510]2    "../../lib/object",
[487]3    "./_Class",
4    "./surveyRuns",
5    "dojo/Deferred",
6    "dojo/_base/declare",
7    "dojo/_base/json",
8    "dojo/_base/lang",
[492]9    "dojo/_base/xhr"
[510]10], function(objectFuns, _Class, surveyRuns, Deferred, declare, json, lang, xhr) {
[487]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;
[443]22        },
[487]23        _deserialize: function(obj) {
24            if (obj._surveyRun) {
25                obj._surveyRun = surveyRuns._doDeserialize(obj._surveyRun);
[443]26            }
[487]27            if (obj.publicationDate) {
[492]28                obj.publicationDate = this._parseDate(obj.publicationDate);
[487]29            }
30        },
31        _serialize: function(obj) {
[510]32            this._convertCheckAndRadio(obj.answers);
[487]33            if (obj._surveyRun) {
34                obj._surveyRun = surveyRuns._doSerialize(obj._surveyRun);
35            }
36            if (obj.publicationDate) {
[492]37                obj.publicationDate = this._formatDate(obj.publicationDate);
[487]38            }
39        },
40        getWithSecret: function(id,secret) {
41            var query = xhr.objectToQuery({secret:secret});
42            return xhr('GET',{
43                url: '/api/open/responses/' + id + '?' + query,
44                handleAs: 'json',
45                contentType: false
[490]46            }).then(lang.hitch(this,'_doDeserialize'),
47                    lang.hitch(this,'_deserializeError'));
[487]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
[490]57            }).then(lang.hitch(this,'_doDeserialize'),
58                    lang.hitch(this,'_deserializeError'));
[487]59        },
60        putWithSecret: function(response,secret) {
61            var query = xhr.objectToQuery({secret:secret});
62            var body = json.toJson(this._doSerialize(response));
63            return xhr('PUT',{
64                url: '/api/open/responses/' + this.getId(response) + '?' + query,
65                handleAs: 'json',
66                contentType: 'application/json',
67                rawBody: body
[490]68            }).then(lang.hitch(this,'_doDeserialize'),
69                    lang.hitch(this,'_deserializeError'));
[487]70        },
71        removeWithSecret: function(response,secret) {
72            var query = xhr.objectToQuery({secret:secret});
73            var rev = this.getRev(response);
74            var body = json.toJson(this._doSerialize(response));
75            var headers = {};
76            if ( rev ) {
77                headers['If-Match'] = '"'+rev+'"';
78            }
79            return xhr('DELETE',{
80                url: '/api/open/responses/' + this.getId(response) + '?' + query,
81                headers: headers,
82                handleAs: 'json',
83                contentType: 'application/json',
84                rawBody: body
[509]85            }).then(function(result){
86                return result;
87            },lang.hitch(this,'_deserializeError'));
[510]88        },
89        _convertCheckAndRadio: function(answers) {
90            // When we encounter an array, we assume it's really a
91            // checkbox value.
92            objectFuns.forEach(answers,function(v,prop){
93                if ( lang.isArray(v) ) {
94                    switch (v.length) {
95                    case 0:
96                    case 1:
97                        answers[prop] = v[0];
98                        break;
99                    default:
100                        throw new Error("Responses cannot exist of array values.");
101                    }
102                }
103            },this);
[443]104        }
[487]105    });
106
107    return new Responses();
108
109});
Note: See TracBrowser for help on using the repository browser.