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

Last change on this file since 490 was 490, checked in by hendrikvanantwerpen, 11 years ago
  • Mark content as dirty to prevent moving away from unsaved data.
  • Better change propagation from lists and our own widgets.
  • Generate notifications for errors and show correct message.
  • Moved all path/url generation to the class stores, not everywhere we use it.
  • Give user always a choice between Save and Save & Close.
  • Better refresh behaviour on form changes and saves.
  • Don't generate duplicate code error when existing object is the one you're storing.
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    "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'),
46                    lang.hitch(this,'_deserializeError'));
47        },
48        postWithSecret: function(response,secret) {
49            var query = xhr.objectToQuery({secret:secret});
50            var body = json.toJson(this._doSerialize(response));
51            return xhr('POST',{
52                url: '/api/open/responses?' + query,
53                handleAs: 'json',
54                contentType: 'application/json',
55                rawBody: body
56            }).then(lang.hitch(this,'_doDeserialize'),
57                    lang.hitch(this,'_deserializeError'));
58        },
59        putWithSecret: function(response,secret) {
60            var query = xhr.objectToQuery({secret:secret});
61            var body = json.toJson(this._doSerialize(response));
62            return xhr('PUT',{
63                url: '/api/open/responses/' + this.getId(response) + '?' + query,
64                handleAs: 'json',
65                contentType: 'application/json',
66                rawBody: body
67            }).then(lang.hitch(this,'_doDeserialize'),
68                    lang.hitch(this,'_deserializeError'));
69        },
70        removeWithSecret: function(response,secret) {
71            var query = xhr.objectToQuery({secret:secret});
72            var rev = this.getRev(response);
73            var body = json.toJson(this._doSerialize(response));
74            var headers = {};
75            if ( rev ) {
76                headers['If-Match'] = '"'+rev+'"';
77            }
78            return xhr('DELETE',{
79                url: '/api/open/responses/' + this.getId(response) + '?' + query,
80                headers: headers,
81                handleAs: 'json',
82                contentType: 'application/json',
83                rawBody: body
84            }).otherwise(lang.hitch(this,'_deserializeError'));
85        }
86    });
87
88    return new Responses();
89
90});
Note: See TracBrowser for help on using the repository browser.