1 | define([ |
---|
2 | "../../lib/object", |
---|
3 | "./_Class", |
---|
4 | "./surveyRuns", |
---|
5 | "dojo/Deferred", |
---|
6 | "dojo/_base/declare", |
---|
7 | "dojo/_base/json", |
---|
8 | "dojo/_base/lang", |
---|
9 | "dojo/_base/xhr" |
---|
10 | ], function(objectFuns, _Class, surveyRuns, Deferred, declare, json, lang, xhr) { |
---|
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 = this._parseDate(obj.publicationDate); |
---|
29 | } |
---|
30 | }, |
---|
31 | _serialize: function(obj) { |
---|
32 | this._convertCheckAndRadio(obj.answers); |
---|
33 | if (obj._surveyRun) { |
---|
34 | obj._surveyRun = surveyRuns._doSerialize(obj._surveyRun); |
---|
35 | } |
---|
36 | if (obj.publicationDate) { |
---|
37 | obj.publicationDate = this._formatDate(obj.publicationDate); |
---|
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 |
---|
46 | }).then(lang.hitch(this,'_doDeserialize'), |
---|
47 | lang.hitch(this,'_deserializeError')); |
---|
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'), |
---|
58 | lang.hitch(this,'_deserializeError')); |
---|
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 |
---|
68 | }).then(lang.hitch(this,'_doDeserialize'), |
---|
69 | lang.hitch(this,'_deserializeError')); |
---|
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 |
---|
85 | }).then(function(result){ |
---|
86 | return result; |
---|
87 | },lang.hitch(this,'_deserializeError')); |
---|
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); |
---|
104 | } |
---|
105 | }); |
---|
106 | |
---|
107 | return new Responses(); |
---|
108 | |
---|
109 | }); |
---|