Skip to content

Commit

Permalink
Add survey.clearInvisibleValues property (default is false). If it is…
Browse files Browse the repository at this point in the history
… true, survey clears all invisible questions on doComplete, before firing onComplete event and sending data to the server: #35
  • Loading branch information
andrewtelnov committed Jul 21, 2016
1 parent 1086996 commit cdf0b3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Survey {
public storeOthersAsComment: boolean = true;
public pages: Array<PageModel> = new Array<PageModel>();
public triggers: Array<SurveyTrigger> = new Array<SurveyTrigger>();
public clearInvisibleValues: boolean = false;
private currentPageValue: PageModel = null;
private valuesHash: HashTable<any> = {};
private variablesHash: HashTable<any> = {};
Expand Down Expand Up @@ -257,6 +258,9 @@ module Survey {
return vPages.indexOf(this.currentPage) == vPages.length - 1;
}
public doComplete() {
if (this.clearInvisibleValues) {
this.clearInvisibleQuestionValues();
}
this.setCookie();
this.setCompleted();
this.onComplete.fire(this, null);
Expand Down Expand Up @@ -518,6 +522,13 @@ module Survey {
}
return val(name);
}
private clearInvisibleQuestionValues() {
var questions = this.getAllQuestions();
for (var i: number = 0; i < questions.length; i++) {
if (questions[i].visible) continue;
this.setValue(questions[i].name, null);
}
}
public getVariable(name: string): any {
if (!name) return null;
return this.variablesHash[name];
Expand Down Expand Up @@ -606,7 +617,7 @@ module Survey {

JsonObject.metaData.addClass("survey", ["locale", "title", "completedHtml:html", "pages", "questions", "triggers:triggers", "surveyId", "surveyPostId", "cookieName", "sendResultOnPageNext:boolean",
"showNavigationButtons:boolean", "showTitle:boolean", "showPageTitles:boolean", "showPageNumbers:boolean", "showQuestionNumbers", "showProgressBar",
"storeOthersAsComment:boolean", "requiredText", "pagePrevText", "pageNextText", "completeText", "questionStartIndex", "questionTitleTemplate"]);
"storeOthersAsComment:boolean", "clearInvisibleValues:boolean", "requiredText", "pagePrevText", "pageNextText", "completeText", "questionStartIndex", "questionTitleTemplate"]);
JsonObject.metaData.setPropertyValues("survey", "pages", "page");
JsonObject.metaData.setPropertyValues("survey", "questions", null, null,
function (obj) { return null; },
Expand Down
12 changes: 12 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,18 @@ module Survey.Tests {
survey.questionTitleTemplate = "{no}) {title} ({require})"
assert.equal(question.fullTitle, "B) My Title (*)");
});
QUnit.test("clearInvisibleValues", function (assert) {
var survey = twoPageSimplestSurvey();
survey.clearInvisibleValues = true;
var question1 = <Survey.Question>survey.pages[0].questions[0];
question1.value = "myValue";
var question2 = <Survey.Question>survey.pages[0].questions[1];
question2.value = "myValue";
question1.visible = false;
survey.doComplete();
assert.equal(question1.value, null, "Clear value of an invisible question");
assert.equal(question2.value, "myValue", "Keep value of a visible question");
});
QUnit.test("merge values", function (assert) {
class MySurvey extends SurveyModel {
constructor() {
Expand Down

0 comments on commit cdf0b3a

Please sign in to comment.