Changeset 487 for Dev/trunk/src/client/qed-client/response.js
- Timestamp:
- 03/05/14 22:44:48 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/client/qed-client/response.js
r477 r487 4 4 "./app/Path", 5 5 "./lib/async", 6 "./model/classes/ Response",7 "./model/classes/ SurveyRun",6 "./model/classes/responses", 7 "./model/classes/surveyRuns", 8 8 "./pages/response", 9 "./store",10 9 "dojo/_base/json", 11 10 "dojo/date", … … 16 15 "./stddeps", 17 16 "dojo/domReady!" 18 ], function(Content, Page, Path, async, Response, SurveyRun, ResponsePage, store, json, date, locale, hash, parser, request) {17 ], function(Content, Page, Path, async, responses, surveyRuns, ResponsePage, json, date, locale, hash, parser, request) { 19 18 parser.parse(); 20 19 Content.startup(); … … 26 25 } 27 26 28 var path = new Path('/: surveyRunId');27 var path = new Path('/:type/:id'); 29 28 var params = path.match(hash()); 30 29 params.options = params.options || {}; 31 32 if ( !params || !params.surveyRunId ) { 30 31 if ( params && params.type === 'surveyRuns' ) { 32 var response = responses.create(); 33 response.surveyRunId = params.id; 34 responses.postWithSecret(response,params.options.secret) 35 .then(setContent,function(err){ error(err.error); }); 36 } else if ( params && params.type === 'responses' ) { 37 responses.getWithSecret(params.id,params.options.secret) 38 .then(setContent,function(err){ error(err.error); }); 39 } else { 33 40 error("Something is wrong with the URL, don't know what survey to show you. Sorry."); 34 return;35 41 } 36 42 37 var surveyRunId = params.surveyRunId; 38 39 function checkDates(surveyRun) { 40 var now = new Date(); 41 var startDate = SurveyRun.StartDate.get(surveyRun); 42 var endDate = SurveyRun.EndDate.get(surveyRun); 43 if ( startDate && date.compare(startDate,now) > 0 ) { 44 error("This survey will start on "+locale.format(startDate,'date')); 45 throw false; 46 } 47 if ( endDate && date.compare(now,endDate) > 0 ) { 48 error("This survey ended on "+locale.format(endDate,'date')); 49 throw false; 50 } 43 function setContent(response) { 44 hash(Path.format("/responses/"+responses.getId(response), 45 {secret:response.secret})); 46 Content.set(new ResponsePage({ 47 response: response 48 })); 51 49 } 52 50 53 request.get('/api/surveyRuns/'+surveyRunId,{handleAs:'json'})54 .then(function(surveyRun){55 checkDates(surveyRun);56 if ( params.options.id ) {57 return params.options.id;58 } else {59 if ( surveyRun.mode === "open") {60 var response = Response.create();61 response.surveyRunId = surveyRunId;62 return request.post('/api/responses',{63 handleAs:'json',64 data:json.toJson(response),65 headers:{"Content-Type": "application/json"}66 }).then(function(res){67 return res.id;68 });69 } else {70 error("Cannot respond to closed survey without response id. Sorry.");71 throw false;72 }73 }74 return surveyRun;75 },function(){76 error("No running survey found for the given id. Sorry.");77 throw false;78 })79 .then(function(responseId){80 hash(Path.format("/"+surveyRunId,{ id: responseId }));81 Content.set(new ResponsePage({82 surveyRunId: surveyRunId,83 options: {84 id: responseId85 }86 }));87 });88 51 });
Note: See TracChangeset
for help on using the changeset viewer.