const app = getApp(); Component({ properties: { data:{ type:Object, observer: function(data){ //console.log(data); if(data.question_choices){ this.user_answers={}; for(var choice of data.question_choices) this.user_answers[choice.choice_id] = choice.user_answer_boolean; } //console.log(this.user_answers); } }, exercise_status:Number, user_exercise_status: Number, is_md:Boolean }, attached(){ console.log(this.data); }, data: { }, methods: { triggerAnswer({answered=1}={}){ console.info("trigger answer"); if(this.answered!=answered){ let {question_id, q_position} = this.data.data; this.triggerEvent("answer", { q_position, question_id, answered}, { bubbles: true}); console.log("triggered"); this.answered = answered; } }, answer_choice_question: function (e) { console.log(e, this.data); let { detail: { value }, currentTarget: { dataset } }=e console.log("answer_question"); console.log(value); console.log(dataset); let exercise_choice_id; if (Array.isArray(value)) { exercise_choice_id = []; for (var i of value) { exercise_choice_id.push(parseInt(i)); } console.log(exercise_choice_id); } else { exercise_choice_id = parseInt(value); console.log(exercise_choice_id); } app.api("exercise_questions.exercise_answers")({ question_id: dataset.question_id, exercise_choice_id }) .then(res => { var answered = 0; if (Array.isArray(exercise_choice_id)) { answered = exercise_choice_id.length>0?1:0; } else if (exercise_choice_id) { answered = 1; } console.log(answered); this.triggerAnswer({answered}); console.log("answer_question"); console.log(res); if (!Array.isArray(exercise_choice_id)) exercise_choice_id = [exercise_choice_id]; for(var choice of this.data.data.question_choices){ if(exercise_choice_id.indexOf(choice.choice_id)==-1) this.user_answers[choice.choice_id]=false; else this.user_answers[choice.choice_id]=true; } console.log(this.user_answers); }) .catch(e => { //console.error(e); let {question_choices} = this.data.data; question_choices = question_choices.map(i=>{ i.user_answer_boolean = this.user_answers[i.choice_id] //console.log(this.user_answers[i.choice_id]); return i; }) this.setData({"data.question_choices":question_choices}) console.log(e); app.showError(e) }); }, } })