Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Created December 17, 2016 22:52
Show Gist options
  • Select an option

  • Save omarkdev/8427f7505a134bf14fe235d0f3697c32 to your computer and use it in GitHub Desktop.

Select an option

Save omarkdev/8427f7505a134bf14fe235d0f3697c32 to your computer and use it in GitHub Desktop.
const quiz = new Vue({
el: "#quiz",
data() {
return {
product: {},
show: {},
choices: {
questions: [],
options: []
},
typeShow: 'questions',
lastShow: 0,
user: {
name: '',
email: '',
city: '',
state: 'SP',
address: ''
}
}
},
methods : {
/**
* Selects the values of the options
*
* @returns {*}
*/
selectValue()
{
var choice = {
title: this.show.title,
values: [],
};
choice.values = this.show.values.filter(function(value){
return value.selected;
});
this.choices.questions.push(choice);
return this.changeQuiz();
},
/**
* Selects the answer of the questions
*
* @param keyAnswer
* @returns {*}
*/
selectAnswer(keyAnswer)
{
var answer = this.show.answers[keyAnswer];
if(!answer)
return alert("Selecione uma resposta válida");
var choice = {
title: this.show.title,
answer: answer.title,
price: answer.price
};
this.choices.options.push(choice);
return this.changeQuiz();
},
/**
* Change the quiz being shown
*
* @returns {*}
*/
changeQuiz()
{
this.lastShow++;
var nextInfo = this.product[this.typeShow][this.lastShow];
if(!nextInfo && this.typeShow == 'options'){
return this.finishQuiz();
}
if(!nextInfo){
this.lastShow = 0;
this.show = this.product.options[this.lastShow];
return this.typeShow = 'options';
}
return this.show = nextInfo;
},
finishQuiz()
{
this.typeShow = 'contact';
}
},
/**
* This hook is not called during server-side rendering.
*/
mounted() {
this.product = window.product;
this.show = this.product.questions[0];
this.typeShow = 'questions';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment