Skip to content

Commit

Permalink
Merge branch 'AsymmetricVentures-coll-set-optimizations'
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulUithol committed Nov 3, 2014
2 parents 5e68d5a + 21b856c commit d8a591e
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -1749,6 +1756,7 @@
}
}
}

return null;
},

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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;
};
Expand All @@ -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 ),
Expand Down Expand Up @@ -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 );

Expand Down

0 comments on commit d8a591e

Please sign in to comment.