File tree Expand file tree Collapse file tree 3 files changed +36
-11
lines changed Expand file tree Collapse file tree 3 files changed +36
-11
lines changed Original file line number Diff line number Diff line change 354354
355355 q .componentInstance .question = model
356356
357+ model .resetOptions ()
358+
357359 questions .push (model)
358360 })
359361 }
Original file line number Diff line number Diff line change 7070 },
7171
7272 mounted () {
73- if (this .question .answer ) {
74- // Set initial answer when we have one
75- this .question .options .forEach (o => {
76- const optionValue = o .choiceValue ()
77-
78- if (this .question .answer === optionValue || (Array .isArray (this .question .answer ) && this .question .answer .indexOf (optionValue) !== - 1 )) {
79- this .toggleAnswer (o)
80- }
81- })
82- }
83-
8473 this .addKeyListener ()
8574 },
8675
Original file line number Diff line number Diff line change @@ -127,6 +127,8 @@ export default class QuestionModel {
127127 if ( this . multiple && ! Array . isArray ( this . answer ) ) {
128128 this . answer = this . answer ? [ this . answer ] : [ ]
129129 }
130+
131+ this . resetOptions ( )
130132 }
131133
132134 getJumpId ( ) {
@@ -158,4 +160,36 @@ export default class QuestionModel {
158160
159161 this . index = index
160162 }
163+
164+ resetOptions ( ) {
165+ if ( this . options ) {
166+ const isArray = Array . isArray ( this . answer )
167+ let numSelected = 0
168+
169+ this . options . forEach ( o => {
170+ const optionValue = o . choiceValue ( )
171+
172+ if ( this . answer === optionValue || ( isArray && this . answer . indexOf ( optionValue ) !== - 1 ) ) {
173+ o . selected = true
174+ ++ numSelected
175+ }
176+ } )
177+
178+ if ( this . allowOther ) {
179+ let otherAnswer = null
180+
181+ if ( isArray ) {
182+ if ( this . answer . length && this . answer . length !== numSelected ) {
183+ otherAnswer = this . answer [ this . answer . length - 1 ]
184+ }
185+ } else if ( this . options . map ( o => o . choiceValue ( ) ) . indexOf ( this . answer ) === - 1 ) {
186+ otherAnswer = this . answer
187+ }
188+
189+ if ( otherAnswer !== null ) {
190+ this . other = otherAnswer
191+ }
192+ }
193+ }
194+ }
161195}
You can’t perform that action at this time.
0 commit comments