Changeset 420 for Dev/branches/rest-dojo-ui/client/qed/main-response.js
- Timestamp:
- 12/16/12 20:07:35 (12 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
Dev/branches/rest-dojo-ui/client/qed/main-response.js
r419 r420 1 1 require([ 2 'dojo/date', 3 'dojo/date/locale', 2 4 'dojo/hash', 3 5 'dojo/parser', 6 'qed/store', 4 7 'qed/app/Content', 5 8 'qed/app/Page', 6 'qed/pages/viewSurvey', 9 'qed/app/Path', 10 'qed/lib/async', 11 'qed/model/classes/Response', 12 'qed/model/classes/SurveyRun', 13 'qed/pages/response', 7 14 'dojo/domReady!', 8 15 'qed/stddeps' 9 ],function( hash,parser,Content,Page,viewSurvey) {16 ],function(date,locale,hash,parser,store,Content,Page,Path,async,Response,SurveyRun,response) { 10 17 parser.parse(); 11 18 Content.startup(); 12 19 13 var match = /^!\/(\w+)$/g.exec(hash()); 14 if ( !match ) { 20 function error(msg) { 15 21 Content.set(new Page({ 16 templateString: "<div> Something is wrong with the URL, don't know what survey to show you. Sorry.</div>"22 templateString: "<div>"+msg+"</div>" 17 23 })); 24 } 25 26 var path = new Path('/:surveyRunId'); 27 var params = path.match(hash()); 28 params.options = params.options || {}; 29 30 if ( !params || !params.surveyRunId ) { 31 error("Something is wrong with the URL, don't know what survey to show you. Sorry."); 18 32 return; 19 33 } 20 var surveyId = match[1];21 34 22 // read options from hash/url 23 // 24 // authenticate 35 var surveyRunId = params.surveyRunId; 25 36 26 Content.set(new viewSurvey({ 27 surveyId: surveyId 28 })); 37 function checkDates(surveyRun) { 38 var now = new Date(); 39 var startDate = SurveyRun.StartDate.get(surveyRun); 40 var endDate = SurveyRun.EndDate.get(surveyRun); 41 if ( startDate && date.compare(startDate,now) > 0 ) { 42 error("This survey will start on "+locale.format(startDate,'date')); 43 throw false; 44 } 45 if ( endDate && date.compare(now,endDate) > 0 ) { 46 error("This survey ended on "+locale.format(endDate,'date')); 47 throw false; 48 } 49 } 50 51 store.get(surveyRunId) 52 .then(function(surveyRun){ 53 checkDates(surveyRun); 54 if ( params.options.id ) { 55 return params.options.id; 56 } else { 57 if ( surveyRun.mode === "open") { 58 var response = Response.create(); 59 response.surveyRunId = surveyRunId; 60 return store.put(response).then(function(res){ 61 return store.getIdentity(res); 62 }); 63 } else { 64 error("Cannot respond to closed survey without response id. Sorry."); 65 throw false; 66 } 67 } 68 return surveyRun; 69 },function(){ 70 error("No running survey found for the given id. Sorry."); 71 throw false; 72 }) 73 .then(function(responseId){ 74 hash(Path.format("/"+surveyRunId,{ id: responseId })); 75 Content.set(new response({ 76 surveyRunId: surveyRunId, 77 options: { 78 id: responseId 79 } 80 })); 81 }); 29 82 });
Note: See TracChangeset
for help on using the changeset viewer.