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

Last change on this file since 492 was 492, checked in by hendrikvanantwerpen, 11 years ago
  • Enable/disable buttons on content change.
  • One place to do date formatting, because it was going wrong again.
  • Serialize questions in survey properly.
  • _ComplexValueMixin consumes submit events, but does trigger outer forms if present.
  • Trigger dialog show/hide for login only after previous effect is finished.
  • Check that documents are actually valid, not just that validator returned a result.
  • Validate email and timestamp formats.
  • Prepared for live runs.
File size: 3.2 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], function(_Class, surveyRuns, Deferred, declare, json, lang, xhr) {
10
11    var Responses = declare([_Class],{
12        _collection: 'responses',
13        _type: 'Response',
14        create: function() {
15            var obj = {
16                type: this._type,
17                answers: {},
18                surveyRunId: null
19            };
20            return obj;
21        },
22        _deserialize: function(obj) {
23            if (obj._surveyRun) {
24                obj._surveyRun = surveyRuns._doDeserialize(obj._surveyRun);
25            }
26            if (obj.publicationDate) {
27                obj.publicationDate = this._parseDate(obj.publicationDate);
28            }
29        },
30        _serialize: function(obj) {
31            if (obj._surveyRun) {
32                obj._surveyRun = surveyRuns._doSerialize(obj._surveyRun);
33            }
34            if (obj.publicationDate) {
35                obj.publicationDate = this._formatDate(obj.publicationDate);
36            }
37        },
38        getWithSecret: function(id,secret) {
39            var query = xhr.objectToQuery({secret:secret});
40            return xhr('GET',{
41                url: '/api/open/responses/' + id + '?' + query,
42                handleAs: 'json',
43                contentType: false
44            }).then(lang.hitch(this,'_doDeserialize'),
45                    lang.hitch(this,'_deserializeError'));
46        },
47        postWithSecret: function(response,secret) {
48            var query = xhr.objectToQuery({secret:secret});
49            var body = json.toJson(this._doSerialize(response));
50            return xhr('POST',{
51                url: '/api/open/responses?' + query,
52                handleAs: 'json',
53                contentType: 'application/json',
54                rawBody: body
55            }).then(lang.hitch(this,'_doDeserialize'),
56                    lang.hitch(this,'_deserializeError'));
57        },
58        putWithSecret: function(response,secret) {
59            var query = xhr.objectToQuery({secret:secret});
60            var body = json.toJson(this._doSerialize(response));
61            return xhr('PUT',{
62                url: '/api/open/responses/' + this.getId(response) + '?' + query,
63                handleAs: 'json',
64                contentType: 'application/json',
65                rawBody: body
66            }).then(lang.hitch(this,'_doDeserialize'),
67                    lang.hitch(this,'_deserializeError'));
68        },
69        removeWithSecret: function(response,secret) {
70            var query = xhr.objectToQuery({secret:secret});
71            var rev = this.getRev(response);
72            var body = json.toJson(this._doSerialize(response));
73            var headers = {};
74            if ( rev ) {
75                headers['If-Match'] = '"'+rev+'"';
76            }
77            return xhr('DELETE',{
78                url: '/api/open/responses/' + this.getId(response) + '?' + query,
79                headers: headers,
80                handleAs: 'json',
81                contentType: 'application/json',
82                rawBody: body
83            }).otherwise(lang.hitch(this,'_deserializeError'));
84        }
85    });
86
87    return new Responses();
88
89});
Note: See TracBrowser for help on using the repository browser.