Skip to content

Commit 17eae52

Browse files
committed
Add mixin for getting question component instances
1 parent baec31d commit 17eae52

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/components/FlowForm.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import QuestionModel, { ChoiceOption, LinkOption, QuestionType } from '../models/QuestionModel'
136136
import LanguageModel from '../models/LanguageModel'
137137
import { IsMobile } from '../mixins/IsMobile'
138+
import { ComponentInstance } from '../mixins/ComponentInstance'
138139
139140
export default {
140141
name: 'FlowForm',
@@ -173,6 +174,7 @@
173174
174175
mixins: [
175176
IsMobile,
177+
ComponentInstance
176178
],
177179
178180
data() {
@@ -299,19 +301,18 @@
299301
descriptionLink: LinkOption
300302
}
301303
302-
window.a = this
303-
304304
this
305305
.$slots
306306
.default()[0]
307307
.children
308308
.filter(q => q.type && q.type.name.indexOf('Question') !== -1)
309309
.forEach(q => {
310-
const props = q.props
310+
const props = q.props
311+
const componentInstance = this.getInstance(props.id)
311312
let model = new QuestionModel()
312313
313-
if (q.question !== undefined) {
314-
model = q.question
314+
if (componentInstance.question !== null) {
315+
model = componentInstance.question
315316
}
316317
317318
if (props.modelValue) {
@@ -363,7 +364,7 @@
363364
}
364365
})
365366
366-
q.question = model
367+
componentInstance.question = model
367368
368369
model.resetOptions()
369370

src/components/Question.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
<script>
2+
import { ComponentInstance } from '../mixins/ComponentInstance'
3+
24
export default {
35
name: 'Question',
6+
7+
mixins: [
8+
ComponentInstance
9+
],
10+
11+
props: {
12+
id: null,
13+
modelValue: [String, Array, Boolean, Number, Object]
14+
},
415
516
data() {
617
return {
718
question: null
819
}
920
},
1021
22+
mounted() {
23+
this.setInstance()
24+
},
25+
1126
render() {
1227
return null
1328
},
1429
1530
watch: {
1631
'question.answer'(val) {
17-
this.$emit('input', val)
32+
this.$emit('update:modelValue', val)
1833
}
1934
}
2035
}

src/components/QuestionTypes/BaseType.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
https://www.ditdot.hr/en
88
*/
99
10-
import QuestionModel, { QuestionType } from '../../models/QuestionModel'
10+
import QuestionModel from '../../models/QuestionModel'
1111
import LanguageModel from '../../models/LanguageModel'
1212
import { IsMobile } from '../../mixins/IsMobile'
1313

src/mixins/ComponentInstance.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright (c) 2020 - present, DITDOT Ltd. - MIT Licence
3+
https://github.com/ditdot-dev/vue-flow-form
4+
https://www.ditdot.hr/en
5+
*/
6+
7+
const instances = {}
8+
9+
export const ComponentInstance = {
10+
methods: {
11+
getInstance(id) {
12+
return instances[id]
13+
},
14+
15+
setInstance() {
16+
instances[this.id] = this
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)