(function(){var e=function(e){var t=this;e.require().script("mvc/model.list","mvc/lang.object").done(function(){var n=function(){var t=e.Object.same;e.Class(e.globalNamespace+".Model.Store",{init:function(){if(this.fullName===e.globalNamespace+".Model.Store")return;this.sets=[],this.data={},this.namespace.bind("destroyed",this.callback("remove")),this.namespace.bind("updated",this.callback("updated"))},updated:function(e,t){var n=this.sets.slice(0),r=["Store - updating "];for(var i=0;i<n.length;i++){var s=n[i],o=this.filter(t,s.params)!==!1,u=s.list.get(t)[0];o&&!u?(r.push("adding to",s.params,"; "),s.list.push(t)):!o&&u&&(r.push("removing from",s.params,"; "),s.list.remove(t.id))}},remove:function(e,t){var n=this.id;t[n]!==undefined&&(t=t[n]);var r=this.data[t];if(!r)return;delete this.data[t]},id:"id",add:function(e,t){var n=e.length,r=0,i,s=this.id,o,u=[];for(;r<n;r++)i=e[r],o=i[s],this.data[o]?this.update(this.data[o],i):u.push(this.data[o]=this.create(i));var a=this.sets.slice(0),f=["Store - adding "];for(var r=0;r<a.length;r++){var l=a[r],c=[];for(var h=0;h<u.length;h++)i=u[h],this.filter(i,l.params)!==!1&&c.push(i);c.length&&(f.push(c.length,"to",l.params,"; "),l.list.push(c))}},update:function(e,t){e.attrs(t.serialize())},create:function(e){return e},has:function(t){return e.Object.subsets(t,this.sets).length},filter:function(e,t){var n,r;for(var n in t){i=0,r=t[n];if(r!==undefined&&e[n]!==undefined&&!this._compare(n,e[n],r))return!1}},compare:{},_compare:function(e,n,r){return t(n,r,this.compare[e])},sort:function(t,n){return e.each((n.order||[]).slice(0).reverse(),function(e,n){var r=n.split(" ");t=t.sort(function(e,t){return r[1].toUpperCase()!=="ASC"?e[r[0]]<t[r[0]]?1:e[r[0]]==t[r[0]]?0:-1:e[r[0]]<t[r[0]]?-1:e[r[0]]==t[r[0]]?0:1})}),t},pagination:function(e,t){var n=parseInt(t.offset,10)||0,r=parseInt(t.limit,10)||e.length-n;return e.slice(n,n+r)},get:function(e){return this.data[e]},findOne:function(t,n,r){if(this.data[t]){if(this.data[t].isRejected)return this.data[t];var i=e.Deferred();i.resolve(this.data[t])}else{var i=this.namespace.findOne({id:t}),s=this;i.done(function(e){s[t]=e})}return i.done(n),i},findAll:function(t,n,r){var i,s=this,o,u=function(){r(o)};typeof n=="function"&&(r=n,n=!1),r=r||function(){};for(var a=0;a<this.sets.length;a++){var f=this.sets[a];if(e.Object.subset(t,f.params,this.compare)){i=f;if(e.Object.same(f.params,t,this.compare)){if(!f.def){var l=this.namespace.findAll(t);f.def=l,l.done(function(e){o=e,s.add(e,t),u()})}else o=f.list,f.def.isResolved()?setTimeout(u,1):f.def.done(u);return f.list}}}o=new this.namespace.List;var c={params:e.extend({},t),list:o};this.sets.push(c);if(i){if(!!i.def)if(i.def.isResolved()){var h=s.findAllCached(t);o.push(h),setTimeout(u,1)}else i.def.done(function(){var e=s.findAllCached(t);o.push(e),u()})}else if(!n){var l=this.namespace.findAll(t);c.def=l,l.done(function(e){s.add(e,t),u()})}return setTimeout(function(){},10),o},findAllCached:function(e){var t=[],n=this.data,r;for(var i in n)r=n[i],this.filter(r,e)!==!1&&t.push(r);return t=this.pagination(this.sort(t,e),e),t}},{})};n(),t.resolveWith(n)})};dispatch("mvc/model.store").containing(e).to("Foundry/2.1 Modules")})();