Ignore:
Timestamp:
03/05/14 22:44:48 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/client/qed-client/pages/response.js

    r478 r487  
    33    "../app/Page",
    44    "../lib/async",
    5     "../model/classes/Response",
    6     "../model/classes/Survey",
    7     "../model/classes/SurveyRun",
    8     "../store",
     5    "../model/classes/responses",
    96    "dojo/_base/declare",
    107    "dojo/_base/event",
     
    1613    "require",
    1714    "dojo/text!./templates/response.html"
    18 ], function(Content, Page, async, Response, Survey, SurveyRun, store, declare, event, json, lang, all, request, when, require, template) {
     15], function(Content, Page, async, responses, declare, event, json, lang, all, request, when, require, template) {
    1916    return declare([Page],{
    2017        contextRequire: require,
     
    2825            this.inherited(arguments);
    2926            this._disableSubmit();
    30             var surveyRunId = this.surveyRunId;
    31             var responseId = this.options && this.options.id;
    32             if ( surveyRunId && responseId ) {
    33                 this._loadSurveyAndResponse(surveyRunId,responseId)
    34                 .then(lang.hitch(this, function() {
    35                     if ( this.response.publicationDate ) {
    36                         this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>");
    37                         this._disableSubmit();
    38                     } else {
    39                         this._enableSubmit();
    40                     }
    41                 }), lang.hitch(this,function() {
    42                     this._showInfo("<div>The url seems to be incorrect, no survey found.</div>");
    43                 }));
     27            if ( !this.response ) {
     28                this._showInfo("<div>The url seems to be incorrect, no response found.</div>");
    4429            } else {
    45                 throw new Error("No valid uid or survey passed!");
     30                this.titleNode.innerHTML = this.response._surveyRun.survey.title || "";
     31                this.surveyWidget.set('survey', this.response._surveyRun.survey);
     32                this.surveyWidget.set('value', this.response.answers || {});
     33                if ( this.response.publicationDate ) {
     34                    this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>");
     35                    this._disableSubmit();
     36                } else {
     37                    this._enableSubmit();
     38                }
    4639            }
    47         },
    48         _loadSurveyAndResponse: function(surveyRunId,responseId){
    49             return all([request.get('/api/surveyRuns/'+surveyRunId,{handleAs:'json'}),
    50                         request.get('/api/responses/'+responseId,{handleAs:'json'})])
    51             .then(lang.hitch(this,function(surveyAndResponse){
    52                 var surveyRun = surveyAndResponse[0];
    53                 this.response = surveyAndResponse[1];
    54                 if ( this.response.surveyRunId !== surveyRunId ) {
    55                     throw "Survey does not match the response...";
    56                 }
    57                 this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey);
    58                 this.surveyWidget.set('survey', surveyRun.survey);
    59                 this.surveyWidget.set('value', this.response.answers || {});
    60             }));
    6140        },
    6241        _enableSubmit: function() {
     
    7857            var answers = this.surveyWidget.get('value');
    7958            this.response.answers = answers;
    80             return request.put('/api/responses/'+store.getIdentity(this.response),{
    81                 handleAs:'json',
    82                 data:json.toJson(this.response),
    83                 headers:{"Content-Type": "application/json"}
    84             }).then(lang.hitch(this,function(res){
    85                 this.response._rev = res.rev;
     59            return responses.putWithSecret(this.response,this.response.secret)
     60            .then(lang.hitch(this,function(response){
     61                this.response = response;
    8662                Content.notify("Your response is saved.");
    8763            }), function(err){
     
    9066        },
    9167        _onSubmit: function(e) {
    92             this.response.publicationDate = store.timestamp();
     68            this.response.publicationDate = new Date();
    9369            this._getAnswersAndSaveResponse()
    9470            .then(lang.hitch(this,function(){
     
    11086            this._disableSubmit();
    11187            this.surveyWidget.destroy();
    112             request('/api/responses/'+store.getIdentity(this.response)+'?rev='+store.getRevision(this.response),{
    113                 method: 'DELETE',
    114                 handleAs:'json',
    115                 data:json.toJson(this.response),
    116                 headers:{"Content-Type": "application/json"}
    117             }).then(lang.hitch(this,function(res){
     88            responses.removeWithSecret(this.response,this.response.secret)
     89            .then(lang.hitch(this,function(res){
    11890                this._showInfo("<div>Your response has been deleted, no answers have been saved.</div>");
    11991                Content.notify("Your response is deleted.");
Note: See TracChangeset for help on using the changeset viewer.