Skip to content

Commit

Permalink
Change override for Collection.remove to Collection._removeModels
Browse files Browse the repository at this point in the history
…; update one test for a changed result.
  • Loading branch information
PaulUithol committed Aug 19, 2015
1 parent 3ec5f9d commit 0731ff3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
16 changes: 6 additions & 10 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -1977,28 +1977,24 @@
};

/**
* Override 'Backbone.Collection.remove' to trigger 'relational:remove'.
* Override 'Backbone.Collection._removeModels' to trigger 'relational:remove'.
*/
var remove = Backbone.Collection.prototype.__remove = Backbone.Collection.prototype.remove;
Backbone.Collection.prototype.remove = function( models, options ) {
var _removeModels = Backbone.Collection.prototype.___removeModels = Backbone.Collection.prototype._removeModels;
Backbone.Collection.prototype._removeModels = function( models, options ) {
// Short-circuit if this Collection doesn't hold RelationalModels
if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) {
return remove.call( this, models, options );
return _removeModels.call( this, models, options );
}

var singular = !_.isArray( models ),
toRemove = [];

models = singular ? ( models ? [ models ] : [] ) : _.clone( models );
options || ( options = {} );
var toRemove = [];

//console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options );
_.each( models, function( model ) {
model = this.get( model ) || ( model && this.get( model.cid ) );
model && toRemove.push( model );
}, this );

var result = remove.call( this, singular ? ( toRemove.length ? toRemove[ 0 ] : null ) : toRemove, options );
var result = _removeModels.call( this, toRemove, options );

_.each( toRemove, function( model ) {
this.trigger( 'relational:remove', model, this, options );
Expand Down
16 changes: 9 additions & 7 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4244,25 +4244,27 @@ $(document).ready(function() {

// Check removing `[]`
var result = cars.remove( [] );
ok( _.isArray( result ) && !result.length, "Removing `[]` is a noop" );
ok( cars.length === 2 );
console.log( result );
ok( result === false, "Removing `[]` is a noop (results in 'false', no models removed)" );
ok( cars.length === 2, "Still 2 cars" );

// Check removing `null`
result = cars.remove( null );
console.log( result );
ok( _.isUndefined( result ), "Removing `null` is a noop" );
ok( cars.length === 2 );
ok( cars.length === 2, "Still 2 cars" );

// Check setting to `[]`
result = cars.set( [] );
ok( _.isArray( result ) && !result.length, "Set `[]` empties collection" );
ok( cars.length === 0 );
ok( cars.length === 0, "All cars gone" );

cars.set( [ e, f ] );
ok( cars.length === 2 );
ok( cars.length === 2, "2 cars again" );

// Check setting `null`
ok( _.isUndefined( cars.set( null ) ), "Set `null` empties collection" );
ok( cars.length === 0 );
ok( cars.length === 0, "All cars gone" );
});

test( "add/remove/set (with `add`, `remove` and `merge` options)", function() {
Expand All @@ -4271,7 +4273,7 @@ $(document).ready(function() {
/**
* Add
*/
coll.add( { id: 1, species: 'giraffe' } );
coll.add( { id: '1', species: 'giraffe' } );

ok( coll.length === 1 );

Expand Down

0 comments on commit 0731ff3

Please sign in to comment.