forked from dhruvaray/backbone-associations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-associations-min.js
12 lines (12 loc) · 5.58 KB
/
backbone-associations-min.js
1
2
3
4
5
6
7
8
9
10
11
12
(function(){var z=this,h,f,w,n,u,v,C,s,D,E;"undefined"===typeof window?(h=require("underscore"),f=require("backbone"),"undefined"!==typeof exports&&(exports=module.exports=f)):(h=z._,f=z.Backbone);w=f.Model;n=f.Collection;u=w.prototype;v=n.prototype;D=/[\.\[\]]+/g;C="change add remove reset sort destroy".split(" ");E=["reset","sort"];f.Associations={VERSION:"0.5.1"};f.Associations.Many=f.Many="Many";f.Associations.One=f.One="One";f.Associations.Self=f.Self="Self";s=f.AssociatedModel=f.Associations.AssociatedModel=
w.extend({relations:void 0,_proxyCalls:void 0,get:function(a){var c=u.get.call(this,a);return c?c:this._getAttr.apply(this,arguments)},set:function(a,c,d){var b;h.isObject(a)||null==a?(b=a,d=c):(b={},b[a]=c);a=this._set(b,d);this._processPendingEvents();return a},_set:function(a,c){var d,b,F,e,l=this;if(!a)return this;for(d in a)if(b||(b={}),d.match(D)){var f=A(d);e=h.initial(f);f=f[f.length-1];e=this.get(e);e instanceof s&&(e=b[e.cid]||(b[e.cid]={model:e,data:{}}),e.data[f]=a[d])}else e=b[this.cid]||
(b[this.cid]={model:this,data:{}}),e.data[d]=a[d];if(b)for(F in b)e=b[F],this._setAttr.call(e.model,e.data,c)||(l=!1);else l=this._setAttr.call(this,a,c);return l},_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&&h.each(this.relations,function(b){var d=b.key,e=b.relatedModel,l=b.collectionType,p=b.map,k=this.attributes[d],q=k&&k.idAttribute,g,t,m,r,B=!1;!e||e.prototype instanceof w||(e=h.isFunction(e)?e.call(this,b,a):e);e&&h.isString(e)&&
(e=e===f.Self?this.constructor:x(e));l&&h.isString(l)&&(l=x(l));p&&h.isString(p)&&(p=x(p));t=b.options?h.extend({},b.options,c):c;if(a[d]){g=h.result(a,d);g=p?p(g):g;if(b.type===f.Many){if(l&&!l.prototype instanceof n)throw Error("collectionType must inherit from Backbone.Collection");g instanceof n?(m=g,B=k!==g):(k?(m=k,m._deferEvents=!0):m=l?new l:this._createCollection(e),m[t.reset?"reset":"set"](g,t))}else b.type===f.One&&e&&(g instanceof s?(m=g,B=k!==g):k?k&&g[q]&&k.get(q)===g[q]?(k._deferEvents=
!0,k._set(g,t),m=k):m=new e(g,t):m=new e(g,t));r=a[d]=m;if(B||r&&!r._proxyCallback)r._proxyCallback=function(){return this._bubbleEvent.call(this,d,r,arguments)},r.on("all",r._proxyCallback,this)}a.hasOwnProperty(d)&&(b=a[d],e=this.attributes[d],b?(b.parents=b.parents||[],-1==h.indexOf(b.parents,this)&&b.parents.push(this)):e&&0<e.parents.length&&(e.parents=h.difference(e.parents,[this]),e._proxyCallback&&e.off("all",e._proxyCallback,this)))},this);return u.set.call(this,a,c)},_bubbleEvent:function(a,
c,d){var b=d[0].split(":"),f=b[0],e="nested-change"==d[0],l=d[1],p=d[2],k=-1,q=c._proxyCalls,g,t=-1!==h.indexOf(C,f);if(!e){1<h.size(b)&&(g=b[1]);-1!==h.indexOf(E,f)&&(p=l);if(c instanceof n&&t&&l){var m=A(g),r=h.initial(m);(b=c.find(function(a){if(l===a)return!0;if(!a)return!1;var b=a.get(r);if((b instanceof s||b instanceof n)&&l===b)return!0;b=a.get(m);if((b instanceof s||b instanceof n)&&l===b||b instanceof n&&p&&p===b)return!0}))&&(k=c.indexOf(b))}g=a+(-1===k||"change"!==f&&!g?"":"["+k+"]")+(g?
"."+g:"");if(/\[\*\]/g.test(g))return this;b=g.replace(/\[\d+\]/g,"[*]");k=[];k.push.apply(k,d);k[0]=f+":"+g;q=c._proxyCalls=q||{};if(this._isEventAvailable.call(this,q,g))return this;q[g]=!0;"change"===f&&(this._previousAttributes[a]=c._previousAttributes,this.changed[a]=c);this.trigger.apply(this,k);"change"===f&&this.get(g)!=d[2]&&this.trigger.apply(this,["nested-change",g,d[1]]);q&&g&&delete q[g];g!==b&&(k[0]=f+":"+b,this.trigger.apply(this,k));return this}},_isEventAvailable:function(a,c){return h.find(a,
function(a,b){return-1!==c.indexOf(b,c.length-b.length)})},_createCollection:function(a){var c=a;h.isString(c)&&(c=x(c));if(c&&c.prototype instanceof s)a=new n,a.model=c;else throw Error("type must inherit from Backbone.AssociatedModel");return a},_processPendingEvents:function(){this.visited||(this.visited=!0,this._deferEvents=!1,h.each(this._pendingEvents,function(a){a.c.trigger.apply(a.c,a.a)}),this._pendingEvents=[],h.each(this.relations,function(a){(a=this.attributes[a.key])&&a._processPendingEvents()},
this),delete this.visited)},trigger:function(a){this._deferEvents?(this._pendingEvents=this._pendingEvents||[],this._pendingEvents.push({c:this,a:arguments})):u.trigger.apply(this,arguments)},toJSON:function(a){var c,d;this.visited||(this.visited=!0,c=u.toJSON.apply(this,arguments),this.relations&&h.each(this.relations,function(b){var f=this.attributes[b.key];f&&(d=f.toJSON(a),c[b.key]=h.isArray(d)?h.compact(d):d)},this),delete this.visited);return c},clone:function(){return new this.constructor(this.toJSON())},
cleanup:function(){h.each(this.relations,function(a){(a=this.attributes[a.key])&&(a.parents=h.difference(a.parents,[this]))},this);this.off()},_getAttr:function(a){var c=this;a=A(a);var d,b;if(!(1>h.size(a))){for(b=0;b<a.length;b++){d=a[b];if(!c)break;c=c instanceof n?isNaN(d)?void 0:c.at(d):c.attributes[d]}return c}}});var G=/[^\.\[\]]+/g,A=function(a){return""===a?[""]:h.isString(a)?a.match(G):a||[]},x=function(a){return h.reduce(a.split("."),function(a,d){return a[d]},z)},H=function(a,c,d){var b;
h.find(a,function(a){if(b=h.find(a.relations,function(b){return a.get(b.key)===c},this))return!0},this);return b&&b.map?b.map(d):d},y={};h.each(["set","remove","reset"],function(a){y[a]=n.prototype[a];v[a]=function(c,d){this.model.prototype instanceof s&&this.parents&&(arguments[0]=H(this.parents,this,c));return y[a].apply(this,arguments)}});y.trigger=v.trigger;v.trigger=function(a){this._deferEvents?(this._pendingEvents=this._pendingEvents||[],this._pendingEvents.push({c:this,a:arguments})):y.trigger.apply(this,
arguments)};v._processPendingEvents=s.prototype._processPendingEvents}).call(this);