Skip to content

Commit

Permalink
Fix: linkedModel values not correctly setup on scale when inherited (
Browse files Browse the repository at this point in the history
…fixes #110).
  • Loading branch information
danielghost committed May 21, 2024
1 parent cd4407e commit 64f9430
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ guide the learner’s interaction with the component.

**labelEnd** (string): Text/characters that appear at the end of the slider scale.

**\_scaleStart** (number): This value is the numeric start of the scale. It is used to calculate the slider's position on the scale.
**\_scaleStart** (number): This value is the numeric start of the scale. It is used to calculate the slider's position on the scale. The default is `1`.

**\_scaleEnd** (number): This value is the numeric end of the scale. It is used to calculate the slider's position on the scale.
**\_scaleEnd** (number): This value is the numeric end of the scale. It is used to calculate the slider's position on the scale. The default is `10`.

**\_scaleStep** (number): Defines the amount the scale should be incremented by.
**\_scaleStep** (number): Defines the amount the scale should be incremented by. The default is `1`.

**\_showNumber** (boolean): When set to `true`, a numeric value appears on the marker described in **\_showScaleIndicator**. The value indicates the slider's position on the scale. The default is `true`. Note that **\_showScaleIndicator** must be set to `true` in order for this to work.

Expand Down
23 changes: 13 additions & 10 deletions js/ConfidenceSliderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export default class ConfidenceSliderModel extends SliderModel {
super.init();
if (!this.get('_linkedToId')) return;
this.set('originalBody', this.get('body'));
this.setupEventListeners();
}

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

/* override */
Expand All @@ -25,10 +20,11 @@ export default class ConfidenceSliderModel extends SliderModel {

/* override to indicate that all options are correct */
setupModelItems() {
this._setupLinkedModel();
const items = [];
const start = this.get('_scaleStart') ?? 1;
const end = this.get('_scaleEnd') ?? 1;
const step = this.get('_scaleStep') || 1;
const start = this.get('_scaleStart');
const end = this.get('_scaleEnd');
const step = this.get('_scaleStep');
for (let i = start; i <= end; i += step) {
items.push({
value: i,
Expand All @@ -45,6 +41,11 @@ export default class ConfidenceSliderModel extends SliderModel {
});
}

onAdaptInitialize() {
this.updateFromLinkedModel();
super.onAdaptInitialize();
}

/* override */
canSubmit() {
return (!this.linkedModel || this.linkedModel.get('_isSubmitted'));
Expand Down Expand Up @@ -116,6 +117,7 @@ export default class ConfidenceSliderModel extends SliderModel {
}

updateFromLinkedModel() {
if (!this.linkedModel) return;
const isSubmitted = this.linkedModel.get('_isSubmitted');
this.set('body', isSubmitted ? this.get('originalBody') : this.get('disabledBody'));
this.set('_isEnabled', isSubmitted);
Expand All @@ -130,7 +132,9 @@ export default class ConfidenceSliderModel extends SliderModel {
}

_setupLinkedModel() {
this.linkedModel = Adapt.components.findWhere({ _id: this.get('_linkedToId') });
const linkedToId = this.get('_linkedToId');
if (!linkedToId) return;
this.linkedModel = Adapt.components.findWhere({ _id: linkedToId });
if (!this.linkedModel) {
return logging.error('Please check that you have set _linkedToId correctly!');
}
Expand All @@ -148,7 +152,6 @@ export default class ConfidenceSliderModel extends SliderModel {
_scaleEnd: this.linkedModel.get('_scaleEnd')
});
this._listenToLinkedModel();
this.updateFromLinkedModel();
if (this.get('_attempts') < 0) this.linkedModel.set('_attempts', 1);
}

Expand Down
2 changes: 1 addition & 1 deletion properties.schema
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"_scaleEnd": {
"type": "number",
"required": true,
"default": 1,
"default": 10,
"title": "Scale End",
"inputType": "Number",
"validators": ["required", "number"],
Expand Down
2 changes: 1 addition & 1 deletion schema/component.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"type": "number",
"title": "Scale End",
"description": "What number the scale should end on",
"default": 1,
"default": 10,
"properties": {}
},
"_scaleStep": {
Expand Down

0 comments on commit 64f9430

Please sign in to comment.