Minimal case showing the problem
Following is a test case that proves the equality in a minimalistic case:
const Child = Backbone.RelationalModel.extend();
const Children = Backbone.Collection.extend({ model: Child });
const Parent = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'children',
relatedModel: Child,
relatedCollection: Children
}]
});
const parent1 = new Parent({ id: 'parent1', children: [{ id: 'child1' }] });
const parent2 = new Parent({ id: 'parent2', children: [{ id: 'child2' }] });
// This is how it is, though not really expected.
expect(parent1.get('children').options).to.equal(parent2.get('children').options);
Potential cause
In the following snippet, collectionOptions gets assigned an empty object, once when loading the library.
|
module.HasMany = module.Relation.extend({ |
|
collectionType: null, |
|
|
|
options: { |
|
reverseRelation: { type: 'HasOne' }, |
|
collectionType: module.Collection, |
|
collectionKey: true, |
|
collectionOptions: {} |
|
}, |
|
|
|
initialize: function( opts ) { |
That object is then reused and passed (in the case it is not a function) as options when instantiating a new collection.
|
_prepareCollection: function( collection ) { |
|
if ( this.related ) { |
|
this.stopListening( this.related ); |
|
} |
|
|
|
if ( !collection || !( collection instanceof module.Collection ) ) { |
|
var options = _.isFunction( this.options.collectionOptions ) ? |
|
this.options.collectionOptions( this.instance ) : this.options.collectionOptions; |
|
|
|
collection = new this.collectionType( null, options ); |
|
} |
This most likely leads to instances of collections sharing a reference to the very same this.options object.
Minimal case showing the problem
Following is a test case that proves the equality in a minimalistic case:
Potential cause
In the following snippet,
collectionOptionsgets assigned an empty object, once when loading the library.Backbone-relational/backbone-relational.js
Lines 932 to 942 in 1ee7dc0
That object is then reused and passed (in the case it is not a function) as options when instantiating a new collection.
Backbone-relational/backbone-relational.js
Lines 967 to 977 in 1ee7dc0
This most likely leads to instances of collections sharing a reference to the very same
this.optionsobject.