Skip to content

Commit

Permalink
Merge pull request #203 from wprl/master
Browse files Browse the repository at this point in the history
Fix to alleviate errors caused by underscore update.
  • Loading branch information
DouweM committed Oct 1, 2012
2 parents 351053c + a182995 commit 1b37060
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@
* @param {Backbone.RelationalModel} modelType
*/
setupSuperModel: function( modelType ) {
_.find( this._subModels, function( subModelDef ) {
return _.find( subModelDef.subModels, function( subModelTypeName, typeValue ) {
_.find( this._subModels || [], function( subModelDef ) {
return _.find( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
var subModelType = this.getObjectByName( subModelTypeName );

if ( modelType === subModelType ) {
Expand All @@ -176,8 +176,8 @@
* @param {String|Object} relation.relatedModel
*/
addReverseRelation: function( relation ) {
var exists = _.any( this._reverseRelations, function( rel ) {
return _.all( relation, function( val, key ) {
var exists = _.any( this._reverseRelations || [], function( rel ) {
return _.all( relation || [], function( val, key ) {
return val === rel[ key ];
});
});
Expand All @@ -191,7 +191,7 @@
}
model.prototype.relations.push( relation );

_.each( model._subModels, function( subModel ) {
_.each( model._subModels || [], function( subModel ) {
addRelation( subModel, relation );
}, this );
};
Expand Down Expand Up @@ -252,8 +252,8 @@
var parts = name.split( '.' ),
type = null;

_.find( this._modelScopes, function( scope ) {
type = _.reduce( parts, function( memo, val ) {
_.find( this._modelScopes || [], function( scope ) {
type = _.reduce( parts || [], function( memo, val ) {
return memo[ val ];
}, scope );

Expand Down Expand Up @@ -518,7 +518,7 @@

// Check if we're not attempting to create a duplicate relationship
if ( i && i._relations.length ) {
var exists = _.any( i._relations, function( rel ) {
var exists = _.any( i._relations || [], function( rel ) {
var hasReverseRelation = this.reverseRelation.key && rel.reverseRelation.key;
return rel.relatedModel === rm && rel.key === k &&
( !hasReverseRelation || this.reverseRelation.key === rel.reverseRelation.key );
Expand Down Expand Up @@ -571,8 +571,8 @@
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 ) {
_.each( models, function( related ) {
_.each( related.getRelations() || [], function( relation ) {
if ( this._isReverseRelation( relation ) ) {
reverseRelations.push( relation );
}
Expand Down Expand Up @@ -621,7 +621,7 @@
.unbind( 'relational:add', this._relatedModelAdded )
.unbind( 'relational:remove', this._relatedModelRemoved );

_.each( this.getReverseRelations(), function( relation ) {
_.each( this.getReverseRelations() || [], function( relation ) {
relation.removeRelated( this.instance );
}, this );
}
Expand All @@ -641,7 +641,7 @@
this.setRelated( model );

// Notify new 'related' object of the new relation.
_.each( this.getReverseRelations(), function( relation ) {
_.each( this.getReverseRelations() || [], function( relation ) {
relation.addRelated( this.instance );
}, this );
},
Expand Down Expand Up @@ -695,15 +695,15 @@

// Notify old 'related' object of the terminated relation
if ( oldRelated && this.related !== oldRelated ) {
_.each( this.getReverseRelations( oldRelated ), function( relation ) {
_.each( this.getReverseRelations( oldRelated ) || [], function( relation ) {
relation.removeRelated( this.instance, options );
}, this );
}

// Notify new 'related' object of the new relation. Note we do re-apply even if this.related is oldRelated;
// that can be necessary for bi-directional relations if 'this.instance' was created after 'this.related'.
// In that case, 'this.instance' will already know 'this.related', but the reverse might not exist yet.
_.each( this.getReverseRelations(), function( relation ) {
_.each( this.getReverseRelations() || [], function( relation ) {
relation.addRelated( this.instance, options );
}, this);

Expand Down Expand Up @@ -848,7 +848,7 @@
this.keyContents = _.isArray( this.keyContents ) ? this.keyContents : [ this.keyContents ];

// Try to find instances of the appropriate 'relatedModel' in the store
_.each( this.keyContents, function( item ) {
_.each( this.keyContents || [], function( item ) {
var model = null;
if ( item instanceof this.relatedModel ) {
model = item;
Expand Down Expand Up @@ -879,7 +879,7 @@
this.keyContents = attr;

// Notify old 'related' object of the terminated relation
_.each( this.getReverseRelations(), function( relation ) {
_.each( this.getReverseRelations() || [], function( relation ) {
relation.removeRelated( this.instance, options );
}, this );

Expand Down Expand Up @@ -907,7 +907,7 @@
}

// Notify new 'related' object of the new relation
_.each( this.getReverseRelations(), function( relation ) {
_.each( this.getReverseRelations() || [], function( relation ) {
relation.addRelated( this.instance, options );
}, this );

Expand All @@ -921,7 +921,7 @@
options = this.sanitizeOptions( options );
if ( !this.related.getByCid( model ) && !this.related.get( model ) ) {
// Check if this new model was specified in 'this.keyContents'
var item = _.any( this.keyContents, function( item ) {
var item = _.any( this.keyContents || [], function( item ) {
var id = Backbone.Relational.store.resolveIdForItem( this.relatedModel, item );
return !_.isNull( id ) && id === model.id;
}, this );
Expand All @@ -946,7 +946,7 @@

options = this.sanitizeOptions( options );

_.each( this.getReverseRelations( model ), function( relation ) {
_.each( this.getReverseRelations( model ) || [], function( relation ) {
relation.addRelated( this.instance, options );
}, this );

Expand All @@ -969,7 +969,7 @@

options = this.sanitizeOptions( options );

_.each( this.getReverseRelations( model ), function( relation ) {
_.each( this.getReverseRelations( model ) || [], function( relation ) {
relation.removeRelated( this.instance, options );
}, this );

Expand Down Expand Up @@ -1085,7 +1085,7 @@
this.acquire(); // Setting up relations often also involve calls to 'set', and we only want to enter this function once
this._relations = [];

_.each( this.relations, function( rel ) {
_.each( this.relations || [], function( rel ) {
var type = !_.isString( rel.type ) ? rel.type : Backbone[ rel.type ] || Backbone.Relational.store.getObjectByName( rel.type );
if ( type && type.prototype instanceof Backbone.Relation ) {
new type( this, rel ); // Also pushes the new Relation into _relations
Expand All @@ -1106,7 +1106,7 @@
*/
updateRelations: function( options ) {
if ( this._isInitialized && !this.isLocked() ) {
_.each( this._relations, function( rel ) {
_.each( this._relations || [], function( rel ) {
// Update from data in `rel.keySource` if set, or `rel.key` otherwise
var val = this.attributes[ rel.keySource ] || this.attributes[ rel.key ];
if ( rel.related !== val ) {
Expand Down Expand Up @@ -1201,7 +1201,7 @@
{
error: function() {
var args = arguments;
_.each( models, function( model ) {
_.each( models || [], function( model ) {
model.trigger( 'destroy', model, model.collection, options );
options.error && options.error.apply( model, args );
});
Expand All @@ -1215,7 +1215,7 @@
requests = [ rel.related.fetch( opts ) ];
}
else {
requests = _.map( models, function( model ) {
requests = _.map( models || [], function( model ) {
var opts = _.defaults(
{
error: function() {
Expand Down Expand Up @@ -1313,7 +1313,7 @@
attributes[ this.idAttribute ] = null;
}

_.each( this.getRelations(), function( rel ) {
_.each( this.getRelations() || [], function( rel ) {
delete attributes[ rel.key ];
});

Expand All @@ -1336,7 +1336,7 @@
json[ this.constructor._subModelTypeAttribute ] = this.constructor._subModelTypeValue;
}

_.each( this._relations, function( rel ) {
_.each( this._relations || [], function( rel ) {
var value = json[ rel.key ];

if ( rel.options.includeInJSON === true) {
Expand Down Expand Up @@ -1413,7 +1413,7 @@
}

// Initialize all reverseRelations that belong to this new model.
_.each( this.prototype.relations, function( rel ) {
_.each( this.prototype.relations || [], function( rel ) {
if ( !rel.model ) {
rel.model = this;
}
Expand Down Expand Up @@ -1478,7 +1478,7 @@
if ( this._superModel ) {
//
if ( this._superModel.prototype.relations ) {
var supermodelRelationsExist = _.any( this.prototype.relations, function( rel ) {
var supermodelRelationsExist = _.any( this.prototype.relations || [], function( rel ) {
return rel.model && rel.model !== this;
}, this );

Expand All @@ -1494,7 +1494,7 @@

// If we came here through 'build' for a model that has 'subModelTypes', and not all of them have been resolved yet, try to resolve each.
if ( this.prototype.subModelTypes && _.keys( this.prototype.subModelTypes ).length !== _.keys( this._subModels ).length ) {
_.each( this.prototype.subModelTypes, function( subModelTypeName ) {
_.each( this.prototype.subModelTypes || [], function( subModelTypeName ) {
var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName );
subModelType && subModelType.initializeModelHierarchy();
});
Expand Down Expand Up @@ -1574,7 +1574,7 @@
var modelsToAdd = [];

//console.debug( 'calling add on coll=%o; model=%o, options=%o', this, models, options );
_.each( models, function( model ) {
_.each( models || [], function( model ) {
if ( !( model instanceof Backbone.Model ) ) {
// `_prepareModel` attempts to find `model` in Backbone.store through `findOrCreate`,
// and sets the new properties on it if is found. Otherwise, a new model is instantiated.
Expand All @@ -1591,7 +1591,7 @@
if ( modelsToAdd.length ) {
add.call( this, modelsToAdd, options );

_.each( modelsToAdd, function( model ) {
_.each( modelsToAdd || [], function( model ) {
this.trigger( 'relational:add', model, this, options );
}, this );
}
Expand All @@ -1613,7 +1613,7 @@
}

//console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options );
_.each( models, function( model ) {
_.each( models || [], function( model ) {
model = this.getByCid( model ) || this.get( model );

if ( model instanceof Backbone.Model ) {
Expand Down

0 comments on commit 1b37060

Please sign in to comment.