Permalink
Browse files

Change override for `Collection.remove` to `Collection._removeModels`…

…; update one test for a changed result.
  • Loading branch information...
PaulUithol committed Aug 19, 2015
1 parent 3ec5f9d commit 0731ff391e3159dfac617e5834b33adef9500b49
Showing with 15 additions and 17 deletions.
  1. +6 −10 backbone-relational.js
  2. +9 −7 test/tests.js
View
@@ -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 );
View
@@ -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() {
@@ -4271,7 +4273,7 @@ $(document).ready(function() {
/**
* Add
*/
coll.add( { id: 1, species: 'giraffe' } );
coll.add( { id: '1', species: 'giraffe' } );
ok( coll.length === 1 );

0 comments on commit 0731ff3

Please sign in to comment.