Skip to content

Commit

Permalink
Ensure parent chapter is set on section before initial sort
Browse files Browse the repository at this point in the history
Chapter positions are needed to sort the global sections
collection. Ensure parent model is already set during initial sort,
such that previous sections can be determined correctly right from the
start.

Otherwise, the outline displays an incorrect transition after
inserting a section since the previous section can not yet be used to
determine available transitions.

REDMINE-20217
  • Loading branch information
tf committed Sep 28, 2023
1 parent cd14c7b commit 94bd748
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 18 additions & 0 deletions package/spec/editor/collections/ForeignKeySubsetCollection-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ describe('ForeignKeySubsetCollection', () => {
expect(postComments.last().post).toBe(post);
});

it('sets reference before intial sort of parent', () => {
const post = new Backbone.Model({id: 5});
const comments = new Backbone.Collection([
], {comparator: 'position'});
const postComments = new ForeignKeySubsetCollection({
parentModel: post,
parent: comments,
foreignKeyAttribute: 'postId',
parentReferenceAttribute: 'post'
});
let referenceOnInitialSort;

comments.once('sort', () => referenceOnInitialSort = comments.first().post);
postComments.add({id: 2})

expect(referenceOnInitialSort).toBe(post);
});

it('removes reference to parent model when model is removed from collection', () => {
const post = new Backbone.Model({id: 5});
const comments = new Backbone.Collection([
Expand Down
16 changes: 8 additions & 8 deletions package/src/editor/collections/ForeignKeySubsetCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export const ForeignKeySubsetCollection = SubsetCollection.extend({

this.autoConsolidatePositions = options.autoConsolidatePositions;

this.listenTo(this, 'add', function(model) {
if (options.parentReferenceAttribute) {
model[options.parentReferenceAttribute] = parentModel;
}

model.set(options.foreignKeyAttribute, parentModel.id);
});

SubsetCollection.prototype.constructor.call(this, {
parent,
parentModel,
Expand All @@ -41,14 +49,6 @@ export const ForeignKeySubsetCollection = SubsetCollection.extend({
}
});

this.listenTo(this, 'add', function(model) {
if (options.parentReferenceAttribute) {
model[options.parentReferenceAttribute] = parentModel;
}

model.set(options.foreignKeyAttribute, parentModel.id);
});

this.listenTo(parentModel, 'destroy dependentDestroy', function() {
this.invoke('trigger', 'dependentDestroy');
this.clear();
Expand Down

0 comments on commit 94bd748

Please sign in to comment.