define(["cilantro/vendor/base"],function(){return Resource=Base.extend({url:"",dataType:"json",timeout:5e3,interval:15,uid:"id",_setup:function(a){if(this.loaded===!0)return;var b=$.isArray(a)?[]:{};this._=$.extend(!0,b,a),this.store={},this.template&&(this.dom=this.render(this.template)),this.loaded=!0},_fetch:function(){var a=this,b={type:"GET",url:this.url,dataType:this.dataType,success:function(b){a._setup(b)}};this.xhr=$.ajax(b)},ready:function(a){a=a||function(){};if(this.loaded)a.apply(this);else{attempts=arguments[1]||parseInt(this.timeout/this.interval),this.store&&!this.xhr&&this._setup(this.store),this.url&&!this.xhr&&this._fetch();if(!--attempts)throw new Error("Failed to get a response");var b=this;setTimeout(function(){b.ready(a,attempts)},15)}return this},render:function(a){arguments.length===1&&typeof a=="object"?(data=a,a=this.template):data=arguments[1]||this._,$.isArray(data)||(data=[data]),typeof a=="string"&&(a=$.jqotec(a));var b,c,d,e,f,g=[];for(var h=0,i=data.length;h<i;h++)e=data[h],c=$.jqote(a,e),d=$(c).data(e),b=this.uid?e[this.uid]:h,this.store[b]=d,g.push.apply(g,d.toArray());return $(g)},grep:function(a,b){var c=arguments[3]||this._;c=$.isArray(c)?c:[c];var d;return this.ready(function(){d=$.grep(c,function(c,d){return $(c).data(a)===b})}),d}}),Resource})