Skip to content

Commit

Permalink
Fix: Body text not updating on revisit with linked confidence sliders (
Browse files Browse the repository at this point in the history
…fixes #101)
  • Loading branch information
kirsty-hames authored Nov 2, 2023
2 parents 8a2eed4 + 7674f32 commit 94bfa9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
22 changes: 21 additions & 1 deletion js/ConfidenceSliderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export default class ConfidenceSliderModel extends SliderModel {
super.init();
if (!this.get('_linkedToId')) return;
this.set('originalBody', this.get('body'));
this._setupLinkedModel();
this.setupEventListeners();
}

setupEventListeners() {
this.listenToOnce(Adapt, 'adapt:initialize', this._setupLinkedModel);
}

/* override */
Expand Down Expand Up @@ -111,6 +115,20 @@ export default class ConfidenceSliderModel extends SliderModel {
return feedbackString;
}

updateFromLinkedModel() {
const isSubmitted = this.linkedModel.get('_isSubmitted');
this.set('body', isSubmitted ? this.get('originalBody') : this.get('disabledBody'));
this.set('_isEnabled', isSubmitted);
if (isSubmitted) {
this.set('_linkedModelSelectedIndex', this.linkedModel.get('_selectedItem').index);
}
this.checkCanSubmit();
}

_listenToLinkedModel() {
this.listenTo(this.linkedModel, 'change:_isSubmitted', this.updateFromLinkedModel);
}

_setupLinkedModel() {
this.linkedModel = Adapt.components.findWhere({ _id: this.get('_linkedToId') });
if (!this.linkedModel) {
Expand All @@ -129,6 +147,8 @@ export default class ConfidenceSliderModel extends SliderModel {
_scaleStart: this.linkedModel.get('_scaleStart'),
_scaleEnd: this.linkedModel.get('_scaleEnd')
});
this._listenToLinkedModel();
this.updateFromLinkedModel();
if (this.get('_attempts') < 0) this.linkedModel.set('_attempts', 1);
}

Expand Down
22 changes: 0 additions & 22 deletions js/ConfidenceSliderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,7 @@ class ConfidenceSliderView extends SliderView {
super.setupQuestion();
}

_listenToLinkedModel() {
this.listenTo(this.model.linkedModel, {
'change:_isSubmitted': this.onLinkedSubmittedChanged
});
}

onQuestionRendered() {
if (this.model.linkedModel) {
this._listenToLinkedModel();
if (!this.model.linkedModel.get('_isSubmitted')) {
this.model.set('_isEnabled', false);
this.model.set('body', this.model.get('disabledBody'));
} else {
this.model.set('_linkedModelSelectedIndex', this.model.linkedModel.get('_selectedItem').index);
}
}
if (!(this.model.get('_isSubmitted') && this.model.has('_userAnswer'))) return;
this.model.set({
feedbackTitle: this.model.get('title'),
Expand All @@ -56,13 +41,6 @@ class ConfidenceSliderView extends SliderView {
super.onNumberSelected(value);
}

onLinkedSubmittedChanged(linkedModel) {
this.model.set('body', this.model.get('originalBody'));
this.model.set('_isEnabled', (linkedModel.get('_isSubmitted') === true));
this.model.set('_linkedModelSelectedIndex', linkedModel.get('_selectedItem').index);
this.model.checkCanSubmit();
}

}

ConfidenceSliderView.template = 'confidenceSlider.jsx';
Expand Down

0 comments on commit 94bfa9f

Please sign in to comment.