define([ 'dojo/_base/declare', 'dojo/_base/event', 'dojo/_base/lang', 'dojo/promise/all', 'dojo/when', '../store', '../app/Page', '../lib/async', '../model/classes/Response', '../model/classes/Survey', '../model/classes/SurveyRun', 'dojo/text!./templates/response.html' ],function(declare,event,lang,all,when,store,Page,async,Response,Survey,SurveyRun,template){ return declare([Page],{ templateString: template, response: null, postCreate: function(){ this.options = this.options || {}; }, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this._disableButtons(); var surveyRunId = this.surveyRunId; var responseId = this.options && this.options.id; if ( surveyRunId && responseId ) { this._loadSurveyAndResponse(surveyRunId,responseId) .then(lang.hitch(this,"_enableButtons")); } else { throw new Error("No valid uid or survey passed!"); } }, _loadSurveyAndResponse: function(surveyRunId,responseId){ return all([store.get(surveyRunId),store.get(responseId)]) .then(lang.hitch(this,function(surveyAndResponse){ var surveyRun = surveyAndResponse[0]; this.response = surveyAndResponse[1]; if ( this.response.surveyRunId !== surveyRunId ) { throw "Survey does not match the response..."; } this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey); this.surveyWidget.set('survey',surveyRun.survey); this.responseForm.set('value',this.response.answers || {}); })); }, _enableButtons: function() { this.submitButton.set('disabled',false); this.continueButton.set('disabled',false); }, _disableButtons: function() { this.submitButton.set('disabled',true); this.continueButton.set('disabled',true); }, _getAnswersAndSaveResponse: function() { this.response.answers = this.responseForm.get('value'); console.log(this.response); return store.put(this.response); }, _onSubmit: function(e) { this.response.publicationDate = store.timestamp(); this._getAnswersAndSaveResponse(); this.content.domNode.innerHTML = "
Thanks for filling in the survey.
"; if ( e ) { event.stop(e); } return false; }, _onContinueLater: function(e) { this._getAnswersAndSaveResponse(); this.content.domNode.innerHTML = "
To continue with this survey later, just save the URL in the location bar and revisit it later. Your answers will still be there.
"; if ( e ) { event.stop(e); } return false; }, _ignoreEvent: function(e) { if ( e ) { event.stop(e); } return false; } }); });