1 | define([ |
---|
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 | }); |
---|