Permalink
Browse files

Don't instantiate models added to a HasMany relation before

calling `set`, to prevent ordering problems. Fixes the failing
test added by #461.
  • Loading branch information...
PaulUithol committed May 15, 2015
1 parent b56db7c commit 84fae6c4ff81ce4cea40ec3345b0fdbb2f781881
Showing with 16 additions and 11 deletions.
  1. +7 −8 backbone-relational.js
  2. +9 −3 test/tests.js
View
@@ -1022,9 +1022,8 @@
}
else {
// If `merge` is true, update models here, instead of during update.
model = this.relatedModel.findOrCreate( attributes,
_.extend( { merge: true }, options, { create: this.options.createModels } )
);
model = ( _.isObject( attributes ) && options.parse && this.relatedModel.prototype.parse ) ?
this.relatedModel.prototype.parse( _.clone( attributes ), options ) : attributes;
}
model && toAdd.push( model );
@@ -1037,9 +1036,9 @@
related = this._prepareCollection();
}
// By now, both `merge` and `parse` will already have been executed for models if they were specified.
// Disable them to prevent additional calls.
related.set( toAdd, _.defaults( { merge: false, parse: false }, options ) );
// By now, `parse` will already have been executed just above for models if specified.
// Disable to prevent additional calls.
related.set( toAdd, _.defaults( { parse: false }, options ) );
}
// Remove entries from `keyIds` that were already part of the relation (and are thus 'unchanged')
@@ -1238,7 +1237,7 @@
var dit = this,
args = arguments;
if ( !Backbone.Relational.eventQueue.isLocked() ) {
if ( !Backbone.Relational.eventQueue.isBlocked() ) {
// If we're not in a more complicated nested scenario, fire the change event right away
Backbone.Model.prototype.trigger.apply( dit, args );
}
@@ -1831,7 +1830,7 @@
findOrCreate: function( attributes, options ) {
options || ( options = {} );
var parsedAttributes = ( _.isObject( attributes ) && options.parse && this.prototype.parse ) ?
this.prototype.parse( _.clone( attributes ) ) : attributes;
this.prototype.parse( _.clone( attributes ), options ) : attributes;
// If specified, use a custom `find` function to match up existing models to the given attributes.
// Otherwise, try to find an instance of 'this' model type in the store
View
@@ -1716,6 +1716,7 @@ $(document).ready(function() {
deepEqual( children.pluck('id'), ['foo1', 'foo2'], 'children are in the right order' );
});
module( "Backbone.RelationalModel inheritance (`subModelTypes`)", { setup: reset } );
test( "Object building based on type, when using explicit collections" , function() {
@@ -3545,7 +3546,7 @@ $(document).ready(function() {
]);
equal(animals.at(0).id, 'lion-1');
deepEqual(events, ['add', 'sort', 'add', 'sort']);
deepEqual(events, ['add', 'add', 'sort']);
});
@@ -3648,8 +3649,8 @@ $(document).ready(function() {
equal( addedModels, 0, "A new visitor should not be forced to go to the zoo!" );
});
module( "Reverse relations", { setup: initObjects } );
@@ -4444,6 +4445,11 @@ $(document).ready(function() {
ok( coll.size() === 1, "One model in coll" );
equal( model.get( 'parent' ), null );
equal( model.get( 'name' ), 'new model' );
deepEqual( model.getIdsToFetch( 'parent' ), [ 'm1' ] );
model = coll.set( { id: 'm2', name: 'updated model', parent: 'm1' } );
equal( model.get( 'name' ), 'updated model' );
deepEqual( model.getIdsToFetch( 'parent' ), [ 'm1' ] );
});

0 comments on commit 84fae6c

Please sign in to comment.