(function(){varv=this,g,h,w,m,r,s,z,o,A,B;"undefined"===typeofwindow?(g=require("underscore"),h=require("backbone"),"undefined"!==typeofexports&&(exports=module.exports=h)):(g=v._,h=v.Backbone);w=h.Model;m=h.Collection;r=w.prototype;s=m.prototype;A=/[\.\[\]]+/g;z="change add remove reset sort destroy".split(" ");B=["reset","sort"];h.Associations={VERSION:"0.5.0"};h.Associations.Many=h.Many="Many";h.Associations.One=h.One="One";o=h.AssociatedModel=h.Associations.AssociatedModel=w.extend({relations:void0,_proxyCalls:void0,get:function(a){varc=r.get.call(this,a);returnc?c:this._getAttr.apply(this,arguments)},set:function(a,c,d){varb;if(g.isObject(a)||a==null){b=a;d=c}else{b={};b[a]=c}a=this._set(b,d);this._processPendingEvents();returna},_set:function(a,c){vard,b,n,f,j=this;if(!a)returnthis;for(dina){b||(b={});if(d.match(A)){vark=x(d);f=g.initial(k);k=k[k.length-1];f=this.get(f);if(finstanceofo){f=b[f.cid]||(b[f.cid]={model:f,data:{}});f.data[k]=a[d]}}else{f=b[this.cid]||(b[this.cid]={model:this,data:{}});f.data[d]=a[d]}}if(b)for(ninb){f=b[n];this._setAttr.call(f.model,f.data,c)||(j=false)}elsej=this._setAttr.call(this,a,c);returnj},_setAttr:function(a,c){vard;c||(c={});if(c.unset)for(dina)a[d]=void0;this.parents=this.parents||[];this.relations&&g.each(this.relations,function(b){vard=b.key,f=b.relatedModel,j=b.collectionType,k=b.map,i=this.attributes[d],y=i&&i.idAttribute,e,q,l,p;f&&g.isString(f)&&(f=t(f));j&&g.isString(j)&&(j=t(j));k&&g.isString(k)&&(k=t(k));q=b.options?g.extend({},b.options,c):c;if(a[d]){e=g.result(a,d);e=k?k(e):e;if(b.type===h.Many){if(j&&!j.prototypeinstanceofm)throwError("collectionType must inherit from Backbone.Collection");if(einstanceofm)l=e;elseif(i){i._deferEvents=true;i.set(e,c);l=i}else{l=j?newj:this._createCollection(f);l.add(e,q)}}elseif(b.type===h.One&&f)if(einstanceofo)l=e;elseif(i)if(i&&e[y]&&i.get(y)===e[y]){i._deferEvents=true;i._set(e,c);l=i}elsel=newf(e,q);elsel=newf(e,q);if((p=a[d]=l)&&!p._proxyCallback){p._proxyCallback=function(){returnthis._bubbleEvent.call(this,d,p,arguments)};p.on("all",p._proxyCallback,this)}}if(a.hasOwnProperty(d)){b=a[d];f=this.attributes[d];if(b){b.parents=b.parents||[];g.indexOf(b.parents,this)==-1&&b.parents.push(this)}elseif(f&&f.parents.length>0)f.parents=g.difference(f.parents,[this])}},this);returnr.set.call(this,a,c)},_bubbleEvent:function(a,c,d){varb=d[0].split(":"),n=b[0],f=d[0]=="nested-change",j=d[1],k=d[2],i=-1,h=c._proxyCalls,e,q=g.indexOf(z,n)!==-1;if(!f){g.size(b)>1&&(e=b[1]);g.indexOf(B,n)!==-1&&(k=j);if(cinstanceofm&&q&&j){varl=x(e),p=g.initial(l);(b=c.find(function(a){if(j===a)returntrue;if(!a)returnfalse;varb=a.get(p);if((binstanceofo||binstanceofm)&&j===b)returntrue;b=a.get(l);if((binstanceofo||binstanceofm)&&j===b||binstanceofm&&k&&k===b)returntrue}))&&(i=c.indexOf(b))}e=a+(i!==-1&&(n==="change"||e)?"["+i+"]":"")+(e?"."+e:"");if(/\[\*\]/g.test(e))returnthis;b=e.replace(/\[\d+\]/g,"[*]");i=[];i.push.apply(i,d);i[0]=n+":"+e;h=c._proxyCalls=h||{};if(this._isEventAvailable.call(this,h,e))returnthis;h[e]=true;if("change"===n){this._previousAttributes[a]=c._previousAttributes;this.changed[a]=c}this.trigger.apply(this,i);"change"===n&&this.get(e)!=d[2]&&this.trigger.apply(this,["nested-change",e,d[1]]);h&&e&&deleteh[e];if(e!==b){i[0]=n+":"+b;this.trigger.apply(this,i)}returnthis}},_isEventAvailable:function(a,c){returng.find(a,function(a,b){returnc.indexOf(b,c.length-b.length)!==-1})},_createCollection:function(a){varc=a;g.isString(c)&&(c=t(c));if(c&&c.prototypeinstanceofo){a=newm;a.model=c}elsethrowError("type must inherit from Backbone.AssociatedModel");returna},_processPendingEvents:function(){if(!this.visited){this.visited=true;this._deferEvents=false;g.each(this._pendingEvents,function(a){a.c.trigger.apply(a.c,a.a)});this._pendingEvents=[];g.each(this.relations,function(a){(a=this.attributes[a.key])&&a._processPendingEvents()},this);deletethis.visited}},trigger:function(a){if(this._deferEvents){this._pendingEvents=this._pendingEvents||[];this._pendingEvents.push({c:this,a:arguments})}elser.trigger.apply(this,arguments)},toJSON:function(a){varc,d;if(!this.visited){this.visited=true;c=r.toJSON.apply(this,arguments);this.relations&&g.each(this.relations,function(b){varh=this.attributes[b.key];if(h){d=h.toJSON(a);c[b.key]=g.isArray(d)?g.compact(d):d}},this);deletethis.visited}returnc},clone:function(){returnnewthis.constructor(this.toJSON())},_getAttr:function(a){varc=this,a=x(a),d,b;if(!(g.size(a)<1)){for(b=0;b<a.length;b++){d=a[b];if(!c)break;c=cinstanceofm?isNaN(d)?void0:c.at(d):c.attributes[d]}returnc}}});varC=/[^\.\[\]]+/g,x=function(a){returna===""?[""]:g.isString(a)?a.match(C):a||[]},t=function(a){returng.reduce(a.split("."),function(a,d){returna[d]},v)},D=function(a,c,d){varb;g.find(a,function(a){if(b=g.find(a.relations,function(b){returna.get(b.key)===c},this))returntrue},this);returnb&&b.map?b.map(d):d},u={};g.each(["set","remove","reset"],function(a){u[a]=m.prototype[a];s[a]=function(c,d){this.model.prototypeinstanceofo&&this.parents&&(arguments[0]=D(this.parents,this,c));returnu[a].apply(this,arguments)}});u.trigger=s.trigger;s.trigger=function(a){if(this._deferEvents){this._pendingEvents=this._pendingEvents||[];this._pendingEvents.push({c:this,a:arguments})}elseu.trigger.apply(this,arguments)};s._processPendingEvents=o.prototype._processPendingEvents}).call(this);