Skip to content

Commit

Permalink
New: _isLocked becomes a lockable attribute (fixes #585)
Browse files Browse the repository at this point in the history
* New: _isLocked becomes a lockable attribute

* Do not lock when setting initial default

* Ensure locked attributes remain locked after deepClone
  • Loading branch information
oliverfoster authored Oct 9, 2024
1 parent 80e2ac1 commit 1cccb8f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 12 additions & 4 deletions js/models/adaptModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export default class AdaptModel extends LockingModel {
};
}

lockedAttributes() {
return {
_isLocked: true
};
}

/**
* Fetch an array representing the relative location of the model to the nearest _trackingId
* @returns {Array<Number, Number>}
Expand Down Expand Up @@ -780,27 +786,27 @@ export default class AdaptModel extends LockingModel {
!previousChild.get('_isComplete') &&
!previousChild.get('_isOptional')
);
child.set('_isLocked', isLockedByPreviousChild);
child.set('_isLocked', isLockedByPreviousChild, { pluginName: 'adapt' });
}, false);
}

setUnlockFirstLocking() {
const children = this.getAvailableChildModels();
const firstChild = children.shift();
const isLockedByFirstChild = (!firstChild.get('_isComplete') && !firstChild.get('_isOptional'));
children.forEach(child => child.set('_isLocked', isLockedByFirstChild));
children.forEach(child => child.set('_isLocked', isLockedByFirstChild, { pluginName: 'adapt' }));
}

setLockLastLocking() {
const children = this.getAvailableChildModels();
const lastChild = children.pop();
const isLockedByChildren = children.some(child => (!child.get('_isComplete') && !child.get('_isOptional')));
lastChild.set('_isLocked', isLockedByChildren);
lastChild.set('_isLocked', isLockedByChildren, { pluginName: 'adapt' });
}

setCustomLocking() {
const children = this.getAvailableChildModels();
children.forEach(child => child.set('_isLocked', this.shouldLock(child)));
children.forEach(child => child.set('_isLocked', this.shouldLock(child), { pluginName: 'adapt' }));
}

shouldLock(child) {
Expand Down Expand Up @@ -850,6 +856,8 @@ export default class AdaptModel extends LockingModel {
const ModelClass = this.constructor;
// Clone the model
const clonedModel = new ModelClass(this.toJSON());
clonedModel._lockedAttributes = { ...this._lockedAttributes };
clonedModel._lockedAttributesValues = { ...this._lockedAttributesValues };
// Run the custom modifier on the clone
if (modifier) {
modifier(clonedModel, this);
Expand Down
4 changes: 3 additions & 1 deletion js/models/lockingModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export default class LockingModel extends Backbone.Model {
const isAttemptingToLock = (lockingValue === attrVal);

if (isAttemptingToLock) {
this.setLockState(attrName, true, { pluginName, skipcheck: true });
if (!isInitialDefault) {
this.setLockState(attrName, true, { pluginName, skipcheck: true });
}
newValues[attrName] = lockingValue;
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion js/models/questionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class QuestionModel extends ComponentModel {
]);
}

lockedAttributes() {
return ComponentModel.resultExtend('lockedAttributes', {
_canSubmit: true
});
}

/**
* Returns a string of the model type group.
* @returns {string}
Expand All @@ -60,7 +66,6 @@ class QuestionModel extends ComponentModel {

init() {
this.setupDefaultSettings();
this.setLocking('_canSubmit', true);
this.updateRawScore();
super.init();
}
Expand Down

0 comments on commit 1cccb8f

Please sign in to comment.