Commit 6c313339 by David Baumgold

Updated Backbone-Associations to v0.5.0

parent 05cb13b1
(function(){var f,h,q,l,p,o,s;"undefined"!==typeof require?(f=require("underscore"),h=require("backbone"),exports=module.exports=h):(f=this._,h=this.Backbone);q=h.Model;l=h.Collection;p=q.prototype;s=/[\.\[\]]+/g;h.Many="Many";h.One="One";o=h.AssociatedModel=q.extend({relations:void 0,_proxyCalls:void 0,get:function(b){var a=p.get.call(this,b);return a?a:this.getAttr.apply(this,arguments)},set:function(b,a,d){var c,k,e,i,g=this;if(f.isObject(b)||b==null){c=b;d=a}else{c={};c[b]=a}if(!c)return this;
for(k in c){e||(e={});if(k.match(s)){b=r(k);a=f.initial(b);b=b[b.length-1];a=this.get(a);if(a instanceof o){a=e[a.cid]||(e[a.cid]={model:a,data:{}});a.data[b]=c[k]}}else{a=e[this.cid]||(e[this.cid]={model:this,data:{}});a.data[k]=c[k]}}if(e)for(i in e){a=e[i];this.setAttr.call(a.model,a.data,d)||(g=false)}else return this.setAttr.call(this,c,d);return g},setAttr:function(b,a){var d;a||(a={});if(a.unset)for(d in b)b[d]=void 0;this.relations&&f.each(this.relations,function(c){var d=c.key,e=c.relatedModel,
i=c.collectionType,g,j,n,m;if(b[d]){g=f.result(b,d);e&&f.isString(e)&&(e=eval(e));i&&f.isString(i)&&(i=eval(i));j=c.options?f.extend({},c.options,a):a;if(c.type===h.Many){if(i&&!i.prototype instanceof l)throw Error("collectionType must inherit from Backbone.Collection");if(g instanceof l)n=g;else{n=i?new i:this._createCollection(e);n.add(g,j)}b[d]=n}else if(c.type===h.One&&e){n=g instanceof o?g:new e(g);b[d]=n}if((m=n)&&!m._proxyCallback){m._proxyCallback=function(){return this._bubbleEvent.call(this,
d,m,arguments)};m.on("all",m._proxyCallback,this)}}},this);return p.set.call(this,b,a)},_bubbleEvent:function(b,a,d){var c=d[0].split(":"),k=c[0],e=d[1],i=-1,g=a._proxyCalls,j;f.size(c)>1&&(j=c[1]);if(a instanceof l&&"change"===k&&e){var h=r(j),m=f.initial(h);(c=a.find(function(a){if(e===a)return true;if(a){var b=a.get(m);if((b instanceof o||b instanceof l)&&e===b)return true;b=a.get(h);return(b instanceof o||b instanceof l)&&e===b}return false}))&&(i=a.indexOf(c))}j=b+(i!==-1?"["+i+"]":"")+(j?"."+
j:"");d[0]=k+":"+j;if(g){if(i=f.find(g,function(a,b){return j.indexOf(b,j.length-b.length)!==-1}))return this}else g=a._proxyCalls={};g[j]=true;if("change"===k){this._previousAttributes[b]=a._previousAttributes;this.changed[b]=a}this.trigger.apply(this,d);j&&g&&delete g[j];return this},_createCollection:function(b){var a=b;f.isString(a)&&(a=eval(a));if(a&&a.prototype instanceof o){b=new l;b.model=a}else throw Error("type must inherit from Backbone.AssociatedModel");return b},toJSON:function(b){var a,
d;if(!this.visited){this.visited=true;a=p.toJSON.apply(this,arguments);this.relations&&f.each(this.relations,function(c){var h=this.attributes[c.key];if(h){d=h.toJSON(b);a[c.key]=f.isArray(d)?f.compact(d):d}},this);delete this.visited}return a},clone:function(){return new this.constructor(this.toJSON())},getAttr:function(b){var a=this,b=r(b),d,c;if(!(f.size(b)<1)){for(c=0;c<b.length;c++){d=b[c];if(!a)break;a=a instanceof l&&!isNaN(d)?a.at(d):a.attributes[d]}return a}}});var t=/[^\.\[\]]+/g,r=function(b){return b===
""?[""]:f.isString(b)?b.match(t):b||[]}}).call(this);
(function(){var v=this,g,h,w,m,r,s,z,o,A,B;"undefined"===typeof window?(g=require("underscore"),h=require("backbone"),"undefined"!==typeof exports&&(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:void 0,
_proxyCalls:void 0,get:function(a){var c=r.get.call(this,a);return c?c:this._getAttr.apply(this,arguments)},set:function(a,c,d){var b;if(g.isObject(a)||a==null){b=a;d=c}else{b={};b[a]=c}a=this._set(b,d);this._processPendingEvents();return a},_set:function(a,c){var d,b,n,f,j=this;if(!a)return this;for(d in a){b||(b={});if(d.match(A)){var k=x(d);f=g.initial(k);k=k[k.length-1];f=this.get(f);if(f instanceof o){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(n in b){f=b[n];this._setAttr.call(f.model,f.data,c)||(j=false)}else j=this._setAttr.call(this,a,c);return j},_setAttr:function(a,c){var d;c||(c={});if(c.unset)for(d in a)a[d]=void 0;this.parents=this.parents||[];this.relations&&g.each(this.relations,function(b){var d=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.prototype instanceof m)throw Error("collectionType must inherit from Backbone.Collection");if(e instanceof m)l=e;else if(i){i._deferEvents=true;i.set(e,c);l=i}else{l=j?new j:this._createCollection(f);l.add(e,q)}}else if(b.type===h.One&&f)if(e instanceof o)l=e;else if(i)if(i&&e[y]&&i.get(y)===e[y]){i._deferEvents=true;i._set(e,c);l=i}else l=new f(e,q);else l=new f(e,q);if((p=a[d]=l)&&!p._proxyCallback){p._proxyCallback=
function(){return this._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)}else if(f&&f.parents.length>0)f.parents=g.difference(f.parents,[this])}},this);return r.set.call(this,a,c)},_bubbleEvent:function(a,c,d){var b=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(c instanceof m&&q&&j){var l=x(e),p=g.initial(l);(b=c.find(function(a){if(j===a)return true;if(!a)return false;var b=a.get(p);if((b instanceof o||b instanceof m)&&j===b)return true;b=a.get(l);if((b instanceof o||b instanceof m)&&j===b||b instanceof m&&k&&k===b)return true}))&&(i=c.indexOf(b))}e=a+(i!==-1&&(n==="change"||e)?"["+i+"]":"")+(e?"."+e:"");if(/\[\*\]/g.test(e))return this;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))return this;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&&delete h[e];if(e!==b){i[0]=n+":"+b;this.trigger.apply(this,i)}return this}},_isEventAvailable:function(a,c){return g.find(a,function(a,b){return c.indexOf(b,c.length-b.length)!==-1})},_createCollection:function(a){var c=a;g.isString(c)&&
(c=t(c));if(c&&c.prototype instanceof o){a=new m;a.model=c}else throw Error("type must inherit from Backbone.AssociatedModel");return a},_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);delete this.visited}},trigger:function(a){if(this._deferEvents){this._pendingEvents=this._pendingEvents||
[];this._pendingEvents.push({c:this,a:arguments})}else r.trigger.apply(this,arguments)},toJSON:function(a){var c,d;if(!this.visited){this.visited=true;c=r.toJSON.apply(this,arguments);this.relations&&g.each(this.relations,function(b){var h=this.attributes[b.key];if(h){d=h.toJSON(a);c[b.key]=g.isArray(d)?g.compact(d):d}},this);delete this.visited}return c},clone:function(){return new this.constructor(this.toJSON())},_getAttr:function(a){var c=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=c instanceof m?isNaN(d)?void 0:c.at(d):c.attributes[d]}return c}}});var C=/[^\.\[\]]+/g,x=function(a){return a===""?[""]:g.isString(a)?a.match(C):a||[]},t=function(a){return g.reduce(a.split("."),function(a,d){return a[d]},v)},D=function(a,c,d){var b;g.find(a,function(a){if(b=g.find(a.relations,function(b){return a.get(b.key)===c},this))return true},this);return b&&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.prototype instanceof
o&&this.parents&&(arguments[0]=D(this.parents,this,c));return u[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})}else u.trigger.apply(this,arguments)};s._processPendingEvents=o.prototype._processPendingEvents}).call(this);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment