diff --git a/backbone-relational.js b/backbone-relational.js index 81e26328..5da4840b 100755 --- a/backbone-relational.js +++ b/backbone-relational.js @@ -748,14 +748,21 @@ getReverseRelations: function( model ) { var reverseRelations = []; // Iterate over 'model', 'this.related.models' (if this.related is a Backbone.Collection), or wrap 'this.related' in an array. - var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] ); - _.each( models || [], function( related ) { - _.each( related.getRelations() || [], function( relation ) { + var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] ), + relations = null, + relation = null; + + for( var i = 0; i < ( models || [] ).length; i++ ) { + relations = models[ i ].getRelations() || []; + + for( var j = 0; j < relations.length; j++ ) { + relation = relations[ j ]; + if ( this._isReverseRelation( relation ) ) { reverseRelations.push( relation ); } - }, this ); - }, this ); + } + } return reverseRelations; }, @@ -1749,6 +1756,7 @@ } } } + return null; }, @@ -1916,7 +1924,7 @@ Backbone.Collection.prototype.set = function( models, options ) { // Short-circuit if this Collection doesn't hold RelationalModels if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) { - return set.apply( this, arguments ); + return set.call( this, models, options ); } if ( options && options.parse ) { @@ -1925,41 +1933,42 @@ var singular = !_.isArray( models ), newModels = [], - toAdd = []; + toAdd = [], + model = null; models = singular ? ( models ? [ models ] : [] ) : _.clone( models ); //console.debug( 'calling add on coll=%o; model=%o, options=%o', this, models, options ); - _.each( models, function( model ) { + for ( var i = 0; i < models.length; i++ ) { + model = models[i]; if ( !( model instanceof Backbone.Model ) ) { model = Backbone.Collection.prototype._prepareModel.call( this, model, options ); } - if ( model ) { toAdd.push( model ); - if ( !( this.get( model ) || this.get( model.cid ) ) ) { newModels.push( model ); } // If we arrive in `add` while performing a `set` (after a create, so the model gains an `id`), // we may get here before `_onModelEvent` has had the chance to update `_byId`. - else if ( model.id != null ) { + else if ( model.id !== null && model.id !== undefined ) { this._byId[ model.id ] = model; } } - }, this ); + } // Add 'models' in a single batch, so the original add will only be called once (and thus 'sort', etc). // If `parse` was specified, the collection and contained models have been parsed now. toAdd = singular ? ( toAdd.length ? toAdd[ 0 ] : null ) : toAdd; var result = set.call( this, toAdd, _.defaults( { merge: false, parse: false }, options ) ); - _.each( newModels, function( model ) { + for ( i = 0; i < newModels.length; i++ ) { + model = newModels[i]; // Fire a `relational:add` event for any model in `newModels` that has actually been added to the collection. if ( this.get( model ) || this.get( model.cid ) ) { this.trigger( 'relational:add', model, this, options ); } - }, this ); + } return result; }; @@ -1971,7 +1980,7 @@ Backbone.Collection.prototype.remove = function( models, options ) { // Short-circuit if this Collection doesn't hold RelationalModels if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) { - return remove.apply( this, arguments ); + return remove.call( this, models, options ); } var singular = !_.isArray( models ), @@ -2059,7 +2068,7 @@ // Override .extend() to automatically call .setup() Backbone.RelationalModel.extend = function( protoProps, classProps ) { - var child = Backbone.Model.extend.apply( this, arguments ); + var child = Backbone.Model.extend.call( this, protoProps, classProps ); child.setup( this );