Last active
December 15, 2017 08:58
-
-
Save Kjaer/3f59b562011d556f78ed3fd20489f5d6 to your computer and use it in GitHub Desktop.
Data Model Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
export class MultipleChoiceAnswerModel{ | |
constructor(rawQuestion){ | |
this.questionIdentifier = rawQuestion.identifier; | |
this.type = rawQuestion.question_type; | |
this.multipleChoiceAllowed = typeof rawQuestion.multiple === "string" ? JSON.parse(rawQuestion.multiple) : rawQuestion.multiple; | |
this.choices = Array.from(rawQuestion.choices); | |
this.userAnswer = null; | |
} | |
} | |
export class TextAnswerModel{ | |
constructor(rawQuestion){ | |
this.questionIdentifier = rawQuestion.identifier; | |
this.type = rawQuestion.question_type; | |
this.multilineAllowed = rawQuestion.multiline; | |
this.userAnswer = null; | |
} | |
} | |
const questionTypes = [ | |
{type :"multiple-choice", answerModel: MultipleChoiceAnswerModel}, | |
{type :"text", answerModel: TextAnswerModel} | |
]; | |
export default class QuestionModel { | |
constructor(rawQuestion){ | |
this.type = rawQuestion.question_type; | |
this.identifier = rawQuestion.identifier; | |
this.questionSentence = rawQuestion.headline; | |
this.description = rawQuestion.description; | |
this.required = rawQuestion.required; | |
this.answer = this.createAnswerModel(rawQuestion); | |
} | |
createAnswerModel(question){ | |
let Model = (questionTypes.find( questionType => questionType.type === question.question_type)).answerModel; | |
return new Model(question); | |
} | |
} | |
//Answer Type Enumaration | |
//In case of changing any property of enumaration, freeze the object. | |
export const answerTypes = Object.freeze({ | |
MULTIPLE :"multiple-choice", | |
TEXT :"text" | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"questions": [ | |
{ | |
"question_type": "multiple-choice", | |
"identifier": "ac9835eb-9efa-4cce-9e35-09987d32e6c8", | |
"headline": "Multiple Choice Question Sentence 1", | |
"description": null, | |
"required": false, | |
"multiple": true, | |
"choices": [ | |
{ | |
"label": "Answer 1", | |
"value": "Answer 1", | |
"selected": false | |
}, | |
{ | |
"label": "Answer 2", | |
"value": "Answer 2", | |
"selected": false | |
}, | |
{ | |
"label": "Answer 3", | |
"value": "Answer 4", | |
"selected": false | |
} | |
] | |
}, | |
{ | |
"question_type": "multiple-choice", | |
"identifier": "af711459-9c79-41b7-a6a1-9035f0bac263", | |
"headline": "Yes No question Sentence 2", | |
"description": null, | |
"required": false, | |
"multiple": false, | |
"choices": [ | |
{ | |
"label": "Yes", | |
"value": 1, | |
"selected": false | |
}, | |
{ | |
"label": "No", | |
"value": 0, | |
"selected": false | |
} | |
] | |
}, | |
{ | |
"question_type": "text", | |
"identifier": "37e8ea44-5061-415a-8da3-d49ddb6ba020", | |
"headline": "Text question sentence", | |
"description": null, | |
"required": false, | |
"multiline": true | |
}, | |
{ | |
"question_type": "text", | |
"identifier": "3a98eb1a-9009-4dc8-9497-0c2c14281c9b", | |
"headline": "Text question sentence", | |
"description": null, | |
"required": false, | |
"multiline": false | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.questions = this.survey.questions.map(question => new QuestionModel(question)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment