Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add browserify support #329

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 47 additions & 47 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Backbone-relational.js 0.8.5
* (c) 2011-2013 Paul Uithol and contributors (https://github.com/PaulUithol/Backbone-relational/graphs/contributors)
*
*
* Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
* For details and documentation: https://github.com/PaulUithol/Backbone-relational.
* Depends on Backbone (and thus on Underscore as well): https://github.com/documentcloud/backbone.
Expand All @@ -14,7 +14,7 @@
* CommonJS shim
**/
var _, Backbone, exports;
if ( typeof window === 'undefined' ) {
if ( typeof window === 'undefined' || ( typeof process !== 'undefined' && process.browser ) ) {
_ = require( 'underscore' );
Backbone = require( 'backbone' );
exports = module.exports = Backbone;
Expand Down Expand Up @@ -211,7 +211,7 @@
return val === rel[ key ];
});
});

if ( !exists && relation.model && relation.type ) {
this._reverseRelations.push( relation );
this._addRelation( relation.model, relation );
Expand Down Expand Up @@ -292,18 +292,18 @@
if ( type instanceof Backbone.RelationalModel ) {
type = type.constructor;
}

var rootModel = type;
while ( rootModel._superModel ) {
rootModel = rootModel._superModel;
}

var coll = _.findWhere( this._collections, { model: rootModel } );

if ( !coll && create !== false ) {
coll = this._createCollection( rootModel );
}

return coll;
},

Expand Down Expand Up @@ -331,20 +331,20 @@

_createCollection: function( type ) {
var coll;

// If 'type' is an instance, take its constructor
if ( type instanceof Backbone.RelationalModel ) {
type = type.constructor;
}

// Type should inherit from Backbone.RelationalModel.
if ( type.prototype instanceof Backbone.RelationalModel ) {
coll = new Backbone.Collection();
coll.model = type;

this._collections.push( coll );
}

return coll;
},

Expand Down Expand Up @@ -382,7 +382,7 @@
find: function( type, item ) {
var id = this.resolveIdForItem( type, item );
var coll = this.getCollection( type );

// Because the found object could be of any of the type's superModel
// types, only return it if it's actually of the type asked for.
if ( coll ) {
Expand Down Expand Up @@ -652,7 +652,7 @@
}
}, this );
}, this );

return reverseRelations;
},

Expand All @@ -669,7 +669,7 @@
else if ( this instanceof Backbone.HasMany ) {
this.setRelated( this._prepareCollection() );
}

_.each( this.getReverseRelations(), function( relation ) {
relation.removeRelated( this.instance );
}, this );
Expand Down Expand Up @@ -739,19 +739,19 @@
}
this.acquire();
options = options ? _.clone( options ) : {};

// 'options.__related' is set by 'addRelated'/'removeRelated'. If it is set, the change
// is the result of a call from a relation. If it's not, the change is the result of
// is the result of a call from a relation. If it's not, the change is the result of
// a 'set' call on this.instance.
var changed = _.isUndefined( options.__related ),
oldRelated = changed ? this.related : options.__related;

if ( changed ) {
this.setKeyContents( attr );
var related = this.findRelated( options );
this.setRelated( related );
}

// Notify old 'related' object of the terminated relation
if ( oldRelated && this.related !== oldRelated ) {
_.each( this.getReverseRelations( oldRelated ), function( relation ) {
Expand All @@ -765,7 +765,7 @@
_.each( this.getReverseRelations(), function( relation ) {
relation.addRelated( this.instance, options );
}, this );

// Fire the 'change:<key>' event if 'related' was updated
if ( !options.silent && this.related !== oldRelated ) {
var dit = this;
Expand Down Expand Up @@ -805,7 +805,7 @@
if ( !this.related ) {
return;
}

if ( model === this.related ) {
var oldRelated = this.related || null;
this.setRelated( null );
Expand All @@ -826,7 +826,7 @@

initialize: function( opts ) {
this.listenTo( this.instance, 'relational:change:' + this.key, this.onChange );

// Handle a custom 'collectionType'
this.collectionType = this.options.collectionType;
if ( _.isString( this.collectionType ) ) {
Expand Down Expand Up @@ -859,10 +859,10 @@
}

collection.model = this.relatedModel;

if ( this.options.collectionKey ) {
var key = this.options.collectionKey === true ? this.options.reverseRelation.key : this.options.collectionKey;

if ( collection[ key ] && collection[ key ] !== this.instance ) {
if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) {
console.warn( 'Relation=%o; collectionKey=%s already exists on collection=%o', this, key, this.options.collectionKey );
Expand All @@ -876,7 +876,7 @@
this.listenTo( collection, 'relational:add', this.handleAddition )
.listenTo( collection, 'relational:remove', this.handleRemoval )
.listenTo( collection, 'relational:reset', this.handleReset );

return collection;
},

Expand Down Expand Up @@ -985,7 +985,7 @@
//console.debug('handleAddition called; args=%o', arguments);
options = options ? _.clone( options ) : {};
this.changed = true;

_.each( this.getReverseRelations( model ), function( relation ) {
relation.addRelated( this.instance, options );
}, this );
Expand All @@ -1005,11 +1005,11 @@
//console.debug('handleRemoval called; args=%o', arguments);
options = options ? _.clone( options ) : {};
this.changed = true;

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

var dit = this;
!options.silent && Backbone.Relational.eventQueue.add( function() {
dit.instance.trigger( 'remove:' + dit.key, model, dit.related, options );
Expand Down Expand Up @@ -1098,7 +1098,7 @@
}

Backbone.Relational.store.processOrphanRelations();

this._queue = new Backbone.BlockingQueue();
this._queue.block();
Backbone.Relational.eventQueue.block();
Expand Down Expand Up @@ -1261,7 +1261,7 @@
var created = [],
models = _.map( idsToFetch, function( id ) {
var model = Backbone.Relational.store.find( rel.relatedModel, id );

if ( !model ) {
var attrs = {};
attrs[ rel.relatedModel.prototype.idAttribute ] = id;
Expand All @@ -1271,7 +1271,7 @@

return model;
}, this );

// Try if the 'collection' can provide a url to fetch a set of models in one request.
if ( rel.related instanceof Backbone.Collection && _.isFunction( rel.related.url ) ) {
setUrl = rel.related.url( models );
Expand Down Expand Up @@ -1314,7 +1314,7 @@
}, this );
}
}

return requests;
},

Expand Down Expand Up @@ -1385,7 +1385,7 @@
// Try to run the global queue holding external events
Backbone.Relational.eventQueue.unblock();
}

return result;
},

Expand All @@ -1403,7 +1403,7 @@

clear: function( options ) {
Backbone.Relational.eventQueue.block();

var result = Backbone.Model.prototype.clear.apply( this, arguments );
this.updateRelations( options );

Expand Down Expand Up @@ -1500,7 +1500,7 @@
delete json[ rel.key ];
}
});

this.release();
return json;
}
Expand Down Expand Up @@ -1533,7 +1533,7 @@
if ( !rel.model ) {
rel.model = this;
}

if ( rel.reverseRelation && rel.model === this ) {
var preInitialize = true;
if ( _.isString( rel.relatedModel ) ) {
Expand All @@ -1558,7 +1558,7 @@
}
}
}, this );

return this;
},

Expand All @@ -1583,7 +1583,7 @@
model = subModelType;
}
}

return new model( attributes, options );
},

Expand Down Expand Up @@ -1671,7 +1671,7 @@
Backbone.Collection.prototype.__prepareModel = Backbone.Collection.prototype._prepareModel;
Backbone.Collection.prototype._prepareModel = function ( attrs, options ) {
var model;

if ( attrs instanceof Backbone.Model ) {
if ( !attrs.collection ) {
attrs.collection = this;
Expand All @@ -1681,20 +1681,20 @@
else {
options || ( options = {} );
options.collection = this;

if ( typeof this.model.findOrCreate !== 'undefined' ) {
model = this.model.findOrCreate( attrs, options );
}
else {
model = new this.model( attrs, options );
}

if ( model && model.isNew() && !model._validate( attrs, options ) ) {
this.trigger( 'invalid', this, attrs, options );
model = false;
}
}

return model;
};

Expand Down Expand Up @@ -1751,7 +1751,7 @@
this.trigger( 'relational:add', model, this, options );
}
}, this );

return this;
};

Expand Down Expand Up @@ -1783,7 +1783,7 @@
this.trigger('relational:remove', model, this, options);
}, this );
}

return this;
};

Expand Down Expand Up @@ -1830,29 +1830,29 @@
if ( eventName === 'add' || eventName === 'remove' || eventName === 'reset' ) {
var dit = this,
args = arguments;

if ( _.isObject( args[ 3 ] ) ) {
args = _.toArray( args );
// the fourth argument is the option object.
// we need to clone it, as it could be modified while we wait on the eventQueue to be unblocked
args[ 3 ] = _.clone( args[ 3 ] );
}

Backbone.Relational.eventQueue.add( function() {
trigger.apply( dit, args );
});
}
else {
trigger.apply( this, arguments );
}

return this;
};

// Override .extend() to automatically call .setup()
Backbone.RelationalModel.extend = function( protoProps, classProps ) {
var child = Backbone.Model.extend.apply( this, arguments );

child.setup( this );

return child;
Expand Down