text: /**
   * 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.
   */
  !function(a){"use strict";var b,c,d;"undefined"==typeof window?(b=require("underscore"),c=require("backbone"),d=module.exports=c):(b=window._,c=window.Backbone,d=window),c.Relational={showWarnings:!0},c.Semaphore={_permitsAvailable:null,_permitsUsed:0,acquire:function(){if(this._permitsAvailable&&this._permitsUsed>=this._permitsAvailable)throw new Error("Max permits acquired");this._permitsUsed++},release:function(){if(0===this._permitsUsed)throw new Error("All permits released");this._permitsUsed--},isLocked:function(){return this._permitsUsed>0},setAvailablePermits:function(a){if(this._permitsUsed>a)throw new Error("Available permits cannot be less than used permits");this._permitsAvailable=a}},c.BlockingQueue=function(){this._queue=[]},b.extend(c.BlockingQueue.prototype,c.Semaphore,{_queue:null,add:function(a){this.isBlocked()?this._queue.push(a):a()},process:function(){for(;this._queue&&this._queue.length;)this._queue.shift()()},block:function(){this.acquire()},unblock:function(){this.release(),this.isBlocked()||this.process()},isBlocked:function(){return this.isLocked()}}),c.Relational.eventQueue=new c.BlockingQueue,c.Store=function(){this._collections=[],this._reverseRelations=[],this._orphanRelations=[],this._subModels=[],this._modelScopes=[d]},b.extend(c.Store.prototype,c.Events,{initializeRelation:function(a,d,e){var f=b.isString(d.type)?c[d.type]||this.getObjectByName(d.type):d.type;f&&f.prototype instanceof c.Relation?new f(a,d,e):c.Relational.showWarnings&&"undefined"!=typeof console&&console.warn("Relation=%o; missing or invalid relation type!",d)},addModelScope:function(a){this._modelScopes.push(a)},removeModelScope:function(a){this._modelScopes=b.without(this._modelScopes,a)},addSubModels:function(a,b){this._subModels.push({superModelType:b,subModels:a})},setupSuperModel:function(a){b.find(this._subModels,function(c){return b.find(c.subModels||[],function(b,d){var e=this.getObjectByName(b);return a===e?(c.superModelType._subModels[d]=a,a._superModel=c.superModelType,a._subModelTypeValue=d,a._subModelTypeAttribute=c.superModelType.prototype.subModelTypeAttribute,!0):void 0},this)},this)},addReverseRelation:function(a){var c=b.any(this._reverseRelations,function(c){return b.all(a||[],function(a,b){return a===c[b]})});!c&&a.model&&a.type&&(this._reverseRelations.push(a),this._addRelation(a.model,a),this.retroFitRelation(a))},addOrphanRelation:function(a){var c=b.any(this._orphanRelations,function(c){return b.all(a||[],function(a,b){return a===c[b]})});!c&&a.model&&a.type&&this._orphanRelations.push(a)},processOrphanRelations:function(){b.each(this._orphanRelations.slice(0),function(a){var d=c.Relational.store.getObjectByName(a.relatedModel);d&&(this.initializeRelation(null,a),this._orphanRelations=b.without(this._orphanRelations,a))},this)},_addRelation:function(a,c){a.prototype.relations||(a.prototype.relations=[]),a.prototype.relations.push(c),b.each(a._subModels||[],function(a){this._addRelation(a,c)},this)},retroFitRelation:function(a){var b=this.getCollection(a.model,!1);b&&b.each(function(b){b instanceof a.model&&new a.type(b,a)},this)},getCollection:function(a,d){a instanceof c.RelationalModel&&(a=a.constructor);for(var e=a;e._superModel;)e=e._superModel;var f=b.findWhere(this._collections,{model:e});return f||d===!1||(f=this._createCollection(e)),f},getObjectByName:function(c){var d=c.split("."),e=null;return b.find(this._modelScopes,function(c){return e=b.reduce(d||[],function(b,c){return b?b[c]:a},c),e&&e!==c?!0:void 0},this),e},_createCollection:function(a){var b;return a instanceof c.RelationalModel&&(a=a.constructor),a.prototype instanceof c.RelationalModel&&(b=new c.Collection,b.model=a,this._collections.push(b)),b},resolveIdForItem:function(a,d){var e=b.isString(d)||b.isNumber(d)?d:null;return null===e&&(d instanceof c.RelationalModel?e=d.id:b.isObject(d)&&(e=d[a.prototype.idAttribute])),e||0===e||(e=null),e},find:function(a,b){var c=this.resolveIdForItem(a,b),d=this.getCollection(a);if(d){var e=d.get(c);if(e instanceof a)return e}return null},register:function(a){var b=this.getCollection(a);if(b){var c=a.collection;b.add(a),this.listenTo(a,"destroy",this.unregister,this),a.collection=c}},checkId:function(a,b){var d=this.getCollection(a),e=d&&d.get(b);if(e&&a!==e)throw c.Relational.showWarnings&&"undefined"!=typeof console&&console.warn("Duplicate id! Old RelationalModel=%o, new RelationalModel=%o",e,a),new Error("Cannot instantiate more than one Backbone.RelationalModel with the same id per type!")},update:function(a){var b=this.getCollection(a);b._onModelEvent("change:"+a.idAttribute,a,b),a.trigger("relational:change:id",a,b)},unregister:function(a){this.stopListening(a,"destroy",this.unregister);var b=this.getCollection(a);b&&b.remove(a)},reset:function(){this.stopListening(),this._collections=[],this._subModels=[],this._modelScopes=[d]}}),c.Relational.store=new c.Store,c.Relation=function(a,d,e){if(this.instance=a,d=b.isObject(d)?d:{},this.reverseRelation=b.defaults(d.reverseRelation||{},this.options.reverseRelation),this.options=b.defaults(d,this.options,c.Relation.prototype.options),this.reverseRelation.type=b.isString(this.reverseRelation.type)?c[this.reverseRelation.type]||c.Relational.store.getObjectByName(this.reverseRelation.type):this.reverseRelation.type,this.key=this.options.key,this.keySource=this.options.keySource||this.key,this.keyDestination=this.options.keyDestination||this.keySource||this.key,this.model=this.options.model||this.instance.constructor,this.relatedModel=this.options.relatedModel,b.isString(this.relatedModel)&&(this.relatedModel=c.Relational.store.getObjectByName(this.relatedModel)),this.checkPreconditions()&&(!this.options.isAutoRelation&&this.reverseRelation.type&&this.reverseRelation.key&&c.Relational.store.addReverseRelation(b.defaults({isAutoRelation:!0,model:this.relatedModel,relatedModel:this.model,reverseRelation:this.options},this.reverseRelation)),a)){var f=this.keySource;f!==this.key&&"object"==typeof this.instance.get(this.key)&&(f=this.key),this.setKeyContents(this.instance.get(f)),this.relatedCollection=c.Relational.store.getCollection(this.relatedModel),this.keySource!==this.key&&this.instance.unset(this.keySource,{silent:!0}),this.instance._relations[this.key]=this,this.initialize(e),this.listenTo(this.instance,"destroy",this.destroy).listenTo(this.relatedCollection,"relational:add relational:change:id",this.tryAddRelated).listenTo(this.relatedCollection,"relational:remove",this.removeRelated),this.options.autoFetch&&this.instance.fetchRelated(this.key,b.isObject(this.options.autoFetch)?this.options.autoFetch:{})}},c.Relation.extend=c.Model.extend,b.extend(c.Relation.prototype,c.Events,c.Semaphore,{options:{createModels:!0,includeInJSON:!0,isAutoRelation:!1,autoFetch:!1,parse:!1},instance:null,key:null,keyContents:null,relatedModel:null,relatedCollection:null,reverseRelation:null,related:null,checkPreconditions:function(){var a=this.instance,d=this.key,e=this.model,f=this.relatedModel,g=c.Relational.showWarnings&&"undefined"!=typeof console;if(!e||!d||!f)return g&&console.warn("Relation=%o: missing model, key or relatedModel (%o, %o, %o).",this,e,d,f),!1;if(!(e.prototype instanceof c.RelationalModel))return g&&console.warn("Relation=%o: model does not inherit from Backbone.RelationalModel (%o).",this,a),!1;if(!(f.prototype instanceof c.RelationalModel))return g&&console.warn("Relation=%o: relatedModel does not inherit from Backbone.RelationalModel (%o).",this,f),!1;if(this instanceof c.HasMany&&this.reverseRelation.type===c.HasMany)return g&&console.warn("Relation=%o: relation is a HasMany, and the reverseRelation is HasMany as well.",this),!1;if(a&&b.keys(a._relations).length){var h=b.find(a._relations,function(a){return a.key===d},this);if(h)return g&&console.warn("Cannot create relation=%o on %o for model=%o: already taken by relation=%o.",this,d,a,h),!1}return!0},setRelated:function(a){this.related=a,this.instance.acquire(),this.instance.attributes[this.key]=a,this.instance.release()},_isReverseRelation:function(a){return a.instance instanceof this.relatedModel&&this.reverseRelation.key===a.key&&this.key===a.reverseRelation.key},getReverseRelations:function(a){var c=[],d=b.isUndefined(a)?this.related&&(this.related.models||[this.related]):[a];return b.each(d||[],function(a){b.each(a.getRelations()||[],function(a){this._isReverseRelation(a)&&c.push(a)},this)},this),c},destroy:function(){this.stopListening(),this instanceof c.HasOne?this.setRelated(null):this instanceof c.HasMany&&this.setRelated(this._prepareCollection()),b.each(this.getReverseRelations(),function(a){a.removeRelated(this.instance)},this)}}),c.HasOne=c.Relation.extend({options:{reverseRelation:{type:"HasMany"}},initialize:function(a){this.listenTo(this.instance,"relational:change:"+this.key,this.onChange);var c=this.findRelated(a);this.setRelated(c),b.each(this.getReverseRelations(),function(b){b.addRelated(this.instance,a)},this)},findRelated:function(a){var c=null;if(a=b.defaults({parse:this.options.parse},a),this.keyContents instanceof this.relatedModel)c=this.keyContents;else if(this.keyContents||0===this.keyContents){var d=b.defaults({create:this.options.createModels},a);c=this.relatedModel.findOrCreate(this.keyContents,d)}return this.related&&(this.keyId=null),c},setKeyContents:function(a){this.keyContents=a,this.keyId=c.Relational.store.resolveIdForItem(this.relatedModel,this.keyContents)},onChange:function(a,d,e){if(!this.isLocked()){this.acquire(),e=e?b.clone(e):{};var f=b.isUndefined(e.__related),g=f?this.related:e.__related;if(f){this.setKeyContents(d);var h=this.findRelated(e);this.setRelated(h)}if(g&&this.related!==g&&b.each(this.getReverseRelations(g),function(a){a.removeRelated(this.instance,null,e)},this),b.each(this.getReverseRelations(),function(a){a.addRelated(this.instance,e)},this),!e.silent&&this.related!==g){var i=this;this.changed=!0,c.Relational.eventQueue.add(function(){i.instance.trigger("change:"+i.key,i.instance,i.related,e,!0),i.changed=!1})}this.release()}},tryAddRelated:function(a,b,c){!this.keyId&&0!==this.keyId||a.id!==this.keyId||(this.addRelated(a,c),this.keyId=null)},addRelated:function(a,c){var d=this;a.queue(function(){if(a!==d.related){var e=d.related||null;d.setRelated(a),d.onChange(d.instance,a,b.defaults({__related:e},c))}})},removeRelated:function(a,c,d){if(this.related&&a===this.related){var e=this.related||null;this.setRelated(null),this.onChange(this.instance,a,b.defaults({__related:e},d))}}}),c.HasMany=c.Relation.extend({collectionType:null,options:{reverseRelation:{type:"HasOne"},collectionType:c.Collection,collectionKey:!0,collectionOptions:{}},initialize:function(a){if(this.listenTo(this.instance,"relational:change:"+this.key,this.onChange),this.collectionType=this.options.collectionType,b.isString(this.collectionType)&&(this.collectionType=c.Relational.store.getObjectByName(this.collectionType)),this.collectionType!==c.Collection&&!(this.collectionType.prototype instanceof c.Collection))throw new Error("`collectionType` must inherit from Backbone.Collection");var d=this.findRelated(a);this.setRelated(d)},_prepareCollection:function(a){if(this.related&&this.stopListening(this.related),!(a&&a instanceof c.Collection)){var d=b.isFunction(this.options.collectionOptions)?this.options.collectionOptions(this.instance):this.options.collectionOptions;a=new this.collectionType(null,d)}if(a.model=this.relatedModel,this.options.collectionKey){var e=this.options.collectionKey===!0?this.options.reverseRelation.key:this.options.collectionKey;a[e]&&a[e]!==this.instance?c.Relational.showWarnings&&"undefined"!=typeof console&&console.warn("Relation=%o; collectionKey=%s already exists on collection=%o",this,e,this.options.collectionKey):e&&(a[e]=this.instance)}return this.listenTo(a,"relational:add",this.handleAddition).listenTo(a,"relational:remove",this.handleRemoval).listenTo(a,"relational:reset",this.handleReset),a},findRelated:function(a){var d=null;if(a=b.defaults({parse:this.options.parse},a),this.keyContents instanceof c.Collection)this._prepareCollection(this.keyContents),d=this.keyContents;else{var e=[];b.each(this.keyContents,function(c){if(c instanceof this.relatedModel)var d=c;else d=this.relatedModel.findOrCreate(c,b.extend({merge:!0},a,{create:this.options.createModels}));d&&e.push(d)},this),d=this.related instanceof c.Collection?this.related:this._prepareCollection(),d.set(e,b.defaults({merge:!1,parse:!1},a))}return this.keyIds=b.difference(this.keyIds,b.pluck(d.models,"id")),d},setKeyContents:function(a){this.keyContents=a instanceof c.Collection?a:null,this.keyIds=[],this.keyContents||!a&&0!==a||(this.keyContents=b.isArray(a)?a:[a],b.each(this.keyContents,function(a){var b=c.Relational.store.resolveIdForItem(this.relatedModel,a);(b||0===b)&&this.keyIds.push(b)},this))},onChange:function(a,d,e){e=e?b.clone(e):{},this.setKeyContents(d),this.changed=!1;var f=this.findRelated(e);if(this.setRelated(f),!e.silent){var g=this;c.Relational.eventQueue.add(function(){g.changed&&(g.instance.trigger("change:"+g.key,g.instance,g.related,e,!0),g.changed=!1)})}},handleAddition:function(a,d,e){e=e?b.clone(e):{},this.changed=!0,b.each(this.getReverseRelations(a),function(a){a.addRelated(this.instance,e)},this);var f=this;!e.silent&&c.Relational.eventQueue.add(function(){f.instance.trigger("add:"+f.key,a,f.related,e)})},handleRemoval:function(a,d,e){e=e?b.clone(e):{},this.changed=!0,b.each(this.getReverseRelations(a),function(a){a.removeRelated(this.instance,null,e)},this);var f=this;!e.silent&&c.Relational.eventQueue.add(function(){f.instance.trigger("remove:"+f.key,a,f.related,e)})},handleReset:function(a,d){var e=this;d=d?b.clone(d):{},!d.silent&&c.Relational.eventQueue.add(function(){e.instance.trigger("reset:"+e.key,e.related,d)})},tryAddRelated:function(a,c,d){var e=b.contains(this.keyIds,a.id);e&&(this.addRelated(a,d),this.keyIds=b.without(this.keyIds,a.id))},addRelated:function(a,c){var d=this;a.queue(function(){d.related&&!d.related.get(a)&&d.related.add(a,b.defaults({parse:!1},c))})},removeRelated:function(a,b,c){this.related.get(a)&&this.related.remove(a,c)}}),c.RelationalModel=c.Model.extend({relations:null,_relations:null,_isInitialized:!1,_deferProcessing:!1,_queue:null,subModelTypeAttribute:"type",subModelTypes:null,constructor:function(a,d){if(d&&d.collection){var e=this,f=this.collection=d.collection;delete d.collection,this._deferProcessing=!0;var g=function(a){a===e&&(e._deferProcessing=!1,e.processQueue(),f.off("relational:add",g))};f.on("relational:add",g),b.defer(function(){g(e)})}c.Relational.store.processOrphanRelations(),this._queue=new c.BlockingQueue,this._queue.block(),c.Relational.eventQueue.block();try{c.Model.apply(this,arguments)}finally{c.Relational.eventQueue.unblock()}},trigger:function(a){if(a.length>5&&0===a.indexOf("change")){var b=this,d=arguments;c.Relational.eventQueue.add(function(){if(b._isInitialized){var e=!0;if("change"===a)e=b.hasChanged();else{var f=a.slice(7),g=b.getRelation(f);g&&(e=d[4]===!0,e?b.changed[f]=d[2]:g.changed||delete b.changed[f])}e&&c.Model.prototype.trigger.apply(b,d)}})}else c.Model.prototype.trigger.apply(this,arguments);return this},initializeRelations:function(a){this.acquire(),this._relations={},b.each(this.relations||[],function(b){c.Relational.store.initializeRelation(this,b,a)},this),this._isInitialized=!0,this.release(),this.processQueue()},updateRelations:function(a){this._isInitialized&&!this.isLocked()&&b.each(this._relations,function(b){var c=this.attributes[b.keySource]||this.attributes[b.key];b.related!==c&&this.trigger("relational:change:"+b.key,this,c,a||{})},this)},queue:function(a){this._queue.add(a)},processQueue:function(){this._isInitialized&&!this._deferProcessing&&this._queue.isBlocked()&&this._queue.unblock()},getRelation:function(a){return this._relations[a]},getRelations:function(){return b.values(this._relations)},fetchRelated:function(a,d,e){d=b.extend({update:!0,remove:!1},d);var f,g=[],h=this.getRelation(a),i=h&&(h.keyIds||(h.keyId||0===h.keyId?[h.keyId]:[]));if(e){var j=h.related instanceof c.Collection?h.related.models:[h.related];b.each(j,function(a){(a.id||0===a.id)&&i.push(a.id)})}if(i&&i.length){var k=[],j=b.map(i,function(a){var b=c.Relational.store.find(h.relatedModel,a);if(!b){var e={};e[h.relatedModel.prototype.idAttribute]=a,b=h.relatedModel.findOrCreate(e,d),k.push(b)}return b},this);if(h.related instanceof c.Collection&&b.isFunction(h.related.url)&&(f=h.related.url(j)),f&&f!==h.related.url()){var l=b.defaults({error:function(){var a=arguments;b.each(k,function(b){b.trigger("destroy",b,b.collection,d),d.error&&d.error.apply(b,a)})},url:f},d);g=[h.related.fetch(l)]}else g=b.map(j,function(a){var c=b.defaults({error:function(){b.contains(k,a)&&(a.trigger("destroy",a,a.collection,d),d.error&&d.error.apply(a,arguments))}},d);return a.fetch(c)},this)}return g},get:function(d){var e=c.Model.prototype.get.call(this,d);if(!this.dotNotation||-1===d.indexOf("."))return e;var f=d.split("."),g=b.reduce(f,function(a,b){if(!(a instanceof c.Model))throw new Error("Attribute must be an instanceof Backbone.Model. Is: "+a+", currentSplit: "+b);return c.Model.prototype.get.call(a,b)},this);if(e!==a&&g!==a)throw new Error("Ambiguous result for '"+d+"'. direct result: "+e+", dotNotation: "+g);return e||g},set:function(a,d,e){c.Relational.eventQueue.block();var f;b.isObject(a)||null==a?(f=a,e=d):(f={},f[a]=d);try{var g=this.id,h=f&&this.idAttribute in f&&f[this.idAttribute];c.Relational.store.checkId(this,h);var i=c.Model.prototype.set.apply(this,arguments);this._isInitialized||this.isLocked()?h&&h!==g&&c.Relational.store.update(this):(this.constructor.initializeModelHierarchy(),c.Relational.store.register(this),this.initializeRelations(e)),f&&this.updateRelations(e)}finally{c.Relational.eventQueue.unblock()}return i},unset:function(a,b){c.Relational.eventQueue.block();var d=c.Model.prototype.unset.apply(this,arguments);return this.updateRelations(b),c.Relational.eventQueue.unblock(),d},clear:function(a){c.Relational.eventQueue.block();var b=c.Model.prototype.clear.apply(this,arguments);return this.updateRelations(a),c.Relational.eventQueue.unblock(),b},clone:function(){var a=b.clone(this.attributes);return b.isUndefined(a[this.idAttribute])||(a[this.idAttribute]=null),b.each(this.getRelations(),function(b){delete a[b.key]}),new this.constructor(a)},toJSON:function(a){if(this.isLocked())return this.id;this.acquire();var d=c.Model.prototype.toJSON.call(this,a);return!this.constructor._superModel||this.constructor._subModelTypeAttribute in d||(d[this.constructor._subModelTypeAttribute]=this.constructor._subModelTypeValue),b.each(this._relations,function(e){var f=d[e.key],g=e.options.includeInJSON,h=null;g===!0?f&&b.isFunction(f.toJSON)&&(h=f.toJSON(a)):b.isString(g)?(f instanceof c.Collection?h=f.pluck(g):f instanceof c.Model&&(h=f.get(g)),g===e.relatedModel.prototype.idAttribute&&(e instanceof c.HasMany?h=h.concat(e.keyIds):e instanceof c.HasOne&&(h=h||e.keyId))):b.isArray(g)?f instanceof c.Collection?(h=[],f.each(function(a){var c={};b.each(g,function(b){c[b]=a.get(b)}),h.push(c)})):f instanceof c.Model&&(h={},b.each(g,function(a){h[a]=f.get(a)})):delete d[e.key],g&&(d[e.keyDestination]=h),e.keyDestination!==e.key&&delete d[e.key]}),this.release(),d}},{setup:function(){return this.prototype.relations=(this.prototype.relations||[]).slice(0),this._subModels={},this._superModel=null,this.prototype.hasOwnProperty("subModelTypes")?c.Relational.store.addSubModels(this.prototype.subModelTypes,this):this.prototype.subModelTypes=null,b.each(this.prototype.relations||[],function(a){if(a.model||(a.model=this),a.reverseRelation&&a.model===this){var d=!0;if(b.isString(a.relatedModel)){var e=c.Relational.store.getObjectByName(a.relatedModel);d=e&&e.prototype instanceof c.RelationalModel}d?c.Relational.store.initializeRelation(null,a):b.isString(a.relatedModel)&&c.Relational.store.addOrphanRelation(a)}},this),this},build:function(a,b){var c=this;if(this.initializeModelHierarchy(),this._subModels&&this.prototype.subModelTypeAttribute in a){var d=a[this.prototype.subModelTypeAttribute],e=this._subModels[d];e&&(c=e)}return new c(a,b)},initializeModelHierarchy:function(){if(b.isUndefined(this._superModel)||b.isNull(this._superModel))if(c.Relational.store.setupSuperModel(this),this._superModel&&this._superModel.prototype.relations){var a=b.select(this._superModel.prototype.relations||[],function(a){return!b.any(this.prototype.relations||[],function(b){return a.relatedModel===b.relatedModel&&a.key===b.key},this)},this);this.prototype.relations=a.concat(this.prototype.relations)}else this._superModel=!1;this.prototype.subModelTypes&&b.keys(this.prototype.subModelTypes).length!==b.keys(this._subModels).length&&b.each(this.prototype.subModelTypes||[],function(a){var b=c.Relational.store.getObjectByName(a);b&&b.initializeModelHierarchy()})},findOrCreate:function(a,d){d||(d={});var e=b.isObject(a)&&d.parse&&this.prototype.parse?this.prototype.parse(a):a,f=c.Relational.store.find(this,e);return b.isObject(a)&&(f&&d.merge!==!1?(delete d.collection,f.set(e,d)):f||d.create===!1||(f=this.build(a,d))),f}}),b.extend(c.RelationalModel.prototype,c.Semaphore),c.Collection.prototype.__prepareModel=c.Collection.prototype._prepareModel,c.Collection.prototype._prepareModel=function(a,b){var d;return a instanceof c.Model?(a.collection||(a.collection=this),d=a):(b||(b={}),b.collection=this,d="undefined"!=typeof this.model.findOrCreate?this.model.findOrCreate(a,b):new this.model(a,b),d&&d.isNew()&&!d._validate(a,b)&&(this.trigger("invalid",this,a,b),d=!1)),d};var e=c.Collection.prototype.__set=c.Collection.prototype.set;c.Collection.prototype.set=function(a,d){if(!(this.model.prototype instanceof c.RelationalModel))return e.apply(this,arguments);d&&d.parse&&(a=this.parse(a,d)),b.isArray(a)||(a=a?[a]:[]);var f=[],g=[];return b.each(a,function(a){a instanceof c.Model||(a=c.Collection.prototype._prepareModel.call(this,a,d)),a&&(g.push(a),this.get(a)||this.get(a.cid)?null!=a.id&&(this._byId[a.id]=a):f.push(a))},this),e.call(this,g,b.defaults({parse:!1},d)),b.each(f,function(a){(this.get(a)||this.get(a.cid))&&this.trigger("relational:add",a,this,d)},this),this};var f=c.Collection.prototype.__remove=c.Collection.prototype.remove;c.Collection.prototype.remove=function(a,d){if(!(this.model.prototype instanceof c.RelationalModel))return f.apply(this,arguments);a=b.isArray(a)?a.slice():[a],d||(d={});var e=[];return b.each(a,function(a){a=this.get(a)||this.get(a.cid),a&&e.push(a)},this),e.length&&(f.call(this,e,d),b.each(e,function(a){this.trigger("relational:remove",a,this,d)},this)),this};var g=c.Collection.prototype.__reset=c.Collection.prototype.reset;c.Collection.prototype.reset=function(a,d){return d=b.extend({merge:!0},d),g.call(this,a,d),this.model.prototype instanceof c.RelationalModel&&this.trigger("relational:reset",this,d),this};var h=c.Collection.prototype.__sort=c.Collection.prototype.sort;c.Collection.prototype.sort=function(a){return h.call(this,a),this.model.prototype instanceof c.RelationalModel&&this.trigger("relational:reset",this,a),this};var i=c.Collection.prototype.__trigger=c.Collection.prototype.trigger;c.Collection.prototype.trigger=function(a){if(!(this.model.prototype instanceof c.RelationalModel))return i.apply(this,arguments);if("add"===a||"remove"===a||"reset"===a){var d=this,e=arguments;b.isObject(e[3])&&(e=b.toArray(e),e[3]=b.clone(e[3])),c.Relational.eventQueue.add(function(){i.apply(d,e)})}else i.apply(this,arguments);return this},c.RelationalModel.extend=function(){var a=c.Model.extend.apply(this,arguments);return a.setup(this),a}}();