define([ "./app/Content", "./app/Page", "./app/Path", "./lib/async", "./model/classes/Response", "./model/classes/SurveyRun", "./pages/response", "./store", "dojo/_base/json", "dojo/date", "dojo/date/locale", "dojo/hash", "dojo/parser", "dojo/request", "./stddeps", "dojo/domReady!" ], function(Content, Page, Path, async, Response, SurveyRun, ResponsePage, store, json, date, locale, hash, parser, request) { parser.parse(); Content.startup(); function error(msg) { Content.set(new Page({ templateString: "
"+msg+"
" })); } var path = new Path('/:surveyRunId'); var params = path.match(hash()); params.options = params.options || {}; if ( !params || !params.surveyRunId ) { error("Something is wrong with the URL, don't know what survey to show you. Sorry."); return; } var surveyRunId = params.surveyRunId; function checkDates(surveyRun) { var now = new Date(); var startDate = SurveyRun.StartDate.get(surveyRun); var endDate = SurveyRun.EndDate.get(surveyRun); if ( startDate && date.compare(startDate,now) > 0 ) { error("This survey will start on "+locale.format(startDate,'date')); throw false; } if ( endDate && date.compare(now,endDate) > 0 ) { error("This survey ended on "+locale.format(endDate,'date')); throw false; } } request.get('/api/surveyRuns/'+surveyRunId,{handleAs:'json'}) .then(function(surveyRun){ checkDates(surveyRun); if ( params.options.id ) { return params.options.id; } else { if ( surveyRun.mode === "open") { var response = Response.create(); response.surveyRunId = surveyRunId; return request.post('/api/responses',{ handleAs:'json', data:json.toJson(response), headers:{"Content-Type": "application/json"} }).then(function(res){ return res.id; }); } else { error("Cannot respond to closed survey without response id. Sorry."); throw false; } } return surveyRun; },function(){ error("No running survey found for the given id. Sorry."); throw false; }) .then(function(responseId){ hash(Path.format("/"+surveyRunId,{ id: responseId })); Content.set(new ResponsePage({ surveyRunId: surveyRunId, options: { id: responseId } })); }); });