!function(){var moduleFactory=function($){var module=this;$.require().script("mvc/model.list","mvc/lang.object").done(function(){var exports=function(){var same=$.Object.same;$.Class($.globalNamespace+".Model.Store",{init:function(){this.fullName!==$.globalNamespace+".Model.Store"&&(this.sets=[],this.data={},this.namespace.bind("destroyed",this.callback("remove")),this.namespace.bind("updated",this.callback("updated")))},updated:function(ev,item){for(var sets=this.sets.slice(0),report=["Store - updating "],i=0;i<sets.length;i++){var set=sets[i],inSet=this.filter(item,set.params)!==!1,inList=set.list.get(item)[0];
inSet&&!inList?(report.push("adding to",set.params,"; "),set.list.push(item)):!inSet&&inList&&(report.push("removing from",set.params,"; "),set.list.remove(item.id))}},remove:function(ev,id){var idProp=this.id;void 0!==id[idProp]&&(id=id[idProp]);var item=this.data[id];item&&delete this.data[id]},id:"id",add:function(items){for(var item,id,len=items.length,i=0,idProp=this.id,added=[];len>i;i++)item=items[i],id=item[idProp],this.data[id]?this.update(this.data[id],item):added.push(this.data[id]=this.create(item));
for(var sets=this.sets.slice(0),report=["Store - adding "],i=0;i<sets.length;i++){for(var set=sets[i],itemsForSet=[],j=0;j<added.length;j++)item=added[j],this.filter(item,set.params)!==!1&&itemsForSet.push(item);itemsForSet.length&&(report.push(itemsForSet.length,"to",set.params,"; "),set.list.push(itemsForSet))}},update:function(currentItem,newItem){currentItem.attrs(newItem.serialize())},create:function(newItem){return newItem},has:function(params){return $.Object.subsets(params,this.sets).length},filter:function(item,params){var param,paramValue;
for(var param in params)if(i=0,paramValue=params[param],void 0!==paramValue&&void 0!==item[param]&&!this._compare(param,item[param],paramValue))return!1},compare:{},_compare:function(prop,itemData,paramData){return same(itemData,paramData,this.compare[prop])},sort:function(items,params){return $.each((params.order||[]).slice(0).reverse(),function(i,name){var split=name.split(" ");items=items.sort(function(a,b){return"ASC"!==split[1].toUpperCase()?a[split[0]]<b[split[0]]?1:a[split[0]]==b[split[0]]?0:-1:a[split[0]]<b[split[0]]?-1:a[split[0]]==b[split[0]]?0:1
})}),items},pagination:function(items,params){var offset=parseInt(params.offset,10)||0,limit=parseInt(params.limit,10)||items.length-offset;return items.slice(offset,offset+limit)},get:function(id){return this.data[id]},findOne:function(id,success){if(this.data[id]){if(this.data[id].isRejected)return this.data[id];var def=$.Deferred();def.resolve(this.data[id])}else{var def=this.namespace.findOne({id:id}),self=this;def.done(function(item){self[id]=item})}return def.done(success),def},findAll:function(params,register,ready){var parentLoadedSet,list,self=this,cb=function(){ready(list)
};"function"==typeof register&&(ready=register,register=!1),ready=ready||function(){};for(var i=0;i<this.sets.length;i++){var set=this.sets[i];if($.Object.subset(params,set.params,this.compare)&&(parentLoadedSet=set,$.Object.same(set.params,params,this.compare))){if(set.def)list=set.list,set.def.isResolved()?setTimeout(cb,1):set.def.done(cb);else{var def=this.namespace.findAll(params);set.def=def,def.done(function(items){list=items,self.add(items,params),cb()})}return set.list}}list=new this.namespace.List;var sameSet={params:$.extend({},params),list:list};
if(this.sets.push(sameSet),parentLoadedSet)if(parentLoadedSet.def)if(parentLoadedSet.def.isResolved()){var items=self.findAllCached(params);list.push(items),setTimeout(cb,1)}else parentLoadedSet.def.done(function(){var items=self.findAllCached(params);list.push(items),cb()});else;else if(register);else{var def=this.namespace.findAll(params);sameSet.def=def,def.done(function(items){self.add(items,params),cb()})}return setTimeout(function(){},10),list},findAllCached:function(params){var item,list=[],data=this.data;
for(var id in data)item=data[id],this.filter(item,params)!==!1&&list.push(item);return list=this.pagination(this.sort(list,params),params)}},{})};exports(),module.resolveWith(exports)})};dispatch("mvc/model.store").containing(moduleFactory).to("Foundry/2.1 Modules")}();