// ========================================================================== // Project: Ember Data // Copyright: ©2011 Living Social Inc. and contributors. // License: Licensed under MIT license (see license.js) // ========================================================================== (function(a){window.DS=Ember.Namespace.create()})({}),function(a){DS.Adapter=Ember.Object.extend({commit:function(a,b){b.updated.eachType(function(b,c){this.updateRecords(a,b,c.slice())},this),b.created.eachType(function(b,c){this.createRecords(a,b,c.slice())},this),b.deleted.eachType(function(b,c){this.deleteRecords(a,b,c.slice())},this)},createRecords:function(a,b,c){c.forEach(function(c){this.createRecord(a,b,c)},this)},updateRecords:function(a,b,c){c.forEach(function(c){this.updateRecord(a,b,c)},this)},deleteRecords:function(a,b,c){c.forEach(function(c){this.deleteRecord(a,b,c)},this)},findMany:function(a,b,c){c.forEach(function(c){this.find(a,b,c)},this)}})}({}),function(a){DS.fixtureAdapter=DS.Adapter.create({find:function(a,b,c){var d=b.FIXTURES;if(d.hasLoaded)return;setTimeout(function(){a.loadMany(b,d),d.hasLoaded=!0},300)},findMany:function(){this.find.apply(this,arguments)},findAll:function(a,b){var c=b.FIXTURES,d=c.map(function(a,b,c){return a.id});a.loadMany(b,d,c)}})}({}),function(a){var b=Ember.get,c=Ember.set,d=Ember.getPath;DS.RESTAdapter=DS.Adapter.extend({createRecord:function(a,c,d){var e=this.rootForType(c),f={};f[e]=b(d,"data"),this.ajax("/"+this.pluralize(e),"POST",{data:f,success:function(b){a.didCreateRecord(d,b[e])}})},createRecords:function(a,c,d){if(b(this,"bulkCommit")===!1)return this._super(a,c,d);var e=this.rootForType(c),f=this.pluralize(e),g={};g[f]=d.map(function(a){return b(a,"data")}),this.ajax("/"+this.pluralize(e),"POST",{data:g,success:function(b){a.didCreateRecords(c,d,b[f])}})},updateRecord:function(a,c,e){var f=d(c,"proto.primaryKey"),g=b(e,f),h=this.rootForType(c),i={};i[h]=b(e,"data");var j=["",this.pluralize(h),g].join("/");this.ajax(j,"PUT",{data:i,success:function(b){a.didUpdateRecord(e,b[h])}})},updateRecords:function(a,c,d){if(b(this,"bulkCommit")===!1)return this._super(a,c,d);var e=this.rootForType(c),f=this.pluralize(e),g={};g[f]=d.map(function(a){return b(a,"data")}),this.ajax("/"+this.pluralize(e),"POST",{data:g,success:function(b){a.didUpdateRecords(d,b[f])}})},deleteRecord:function(a,c,e){var f=d(c,"proto.primaryKey"),g=b(e,f),h=this.rootForType(c),i=["",this.pluralize(h),g].join("/");this.ajax(i,"DELETE",{success:function(b){a.didDeleteRecord(e)}})},deleteRecords:function(a,c,e){if(b(this,"bulkCommit")===!1)return this._super(a,c,e);var f=this.rootForType(c),g=this.pluralize(f),h=d(c,"proto.primaryKey"),i={};i[g]=e.map(function(a){return b(a,h)}),this.ajax("/"+this.pluralize(f)+"/delete","POST",{data:i,success:function(b){a.didDeleteRecords(e)}})},find:function(a,b,c){var d=this.rootForType(b),e=["",this.pluralize(d),c].join("/");this.ajax(e,"GET",{success:function(c){a.load(b,c[d])}})},findMany:function(a,b,c){var d=this.rootForType(b),e=this.pluralize(d);this.ajax("/"+e,"GET",{data:{ids:c},success:function(d){a.loadMany(b,c,d[e])}});var f="/"+e},findAll:function(a,b){var c=this.rootForType(b),d=this.pluralize(c);this.ajax("/"+d,"GET",{success:function(c){a.loadMany(b,c[d])}})},findQuery:function(a,b,c,d){var e=this.rootForType(b),f=this.pluralize(e);this.ajax("/"+f,"GET",{data:c,success:function(a){d.load(a[f])}})},plurals:{},pluralize:function(a){return this.plurals[a]||a+"s"},rootForType:function(a){if(a.url)return a.url;var b=a.toString().split("."),c=b[b.length-1];return c.replace(/([A-Z])/g,"_$1").toLowerCase().slice(1)},ajax:function(a,b,c){c.url=a,c.type=b,c.dataType="json",jQuery.ajax(c)}})}({}),function(a){var b=Ember.get,c=Ember.set;DS.ModelArray=Ember.ArrayProxy.extend({type:null,content:null,store:null,init:function(){c(this,"modelCache",Ember.A([])),this._super()},arrayDidChange:function(a,c,d,e){var f=b(this,"modelCache");f.replace(c,0,Array(e)),this._super(a,c,d,e)},arrayWillChange:function(a,c,d,e){this._super(a,c,d,e);var f=b(this,"modelCache");f.replace(c,d)},objectAtContent:function(a){var c=b(this,"modelCache"),d=c.objectAt(a);if(!d){var e=b(this,"store"),f=b(this,"content"),g=f.objectAt(a);g!==undefined&&(d=e.findByClientId(b(this,"type"),g),c.replace(a,1,[d]))}return d}}),DS.FilteredModelArray=DS.ModelArray.extend({filterFunction:null,updateFilter:Ember.observer(function(){var a=b(this,"store");a.updateModelArrayFilter(this,b(this,"type"),b(this,"filterFunction"))},"filterFunction")}),DS.AdapterPopulatedModelArray=DS.ModelArray.extend({query:null,isLoaded:!1,load:function(a){var d=b(this,"store"),e=b(this,"type"),f=d.loadMany(e,a).clientIds;this.beginPropertyChanges(),c(this,"content",Ember.A(f)),c(this,"isLoaded",!0),this.endPropertyChanges()}})}({}),function(a){var b=Ember.get,c=Ember.set,d=Ember.getPath,e=Ember.String.fmt,f=Ember.Object.extend({init:function(){this.clear()},clear:function(){this.set("presenceSet",{}),this.set("list",Ember.NativeArray.apply([]))},add:function(a){var c=Ember.guidFor(a),d=b(this,"presenceSet"),e=b(this,"list");if(c in d)return;d[c]=!0,e.pushObject(a)},remove:function(a){var c=Ember.guidFor(a),d=b(this,"presenceSet"),e=b(this,"list");delete d[c],e.removeObject(a)},isEmpty:function(){return d(this,"list.length")===0},forEach:function(a,c){b(this,"list").forEach(function(b){a.call(c,b)})},toArray:function(){return b(this,"list").slice()}}),g=Ember.Object.extend({init:function(){c(this,"keys",f.create()),c(this,"values",{})},add:function(a,c){var d=b(this,"keys"),e=b(this,"values"),f=Ember.guidFor(a);return d.add(a),e[f]=c,c},remove:function(a){var c=b(this,"keys"),d=b(this,"values"),e=Ember.guidFor(a),f;return c.remove(a),f=d[e],delete d[e],f},fetch:function(a){var c=b(this,"values"),d=Ember.guidFor(a);return c[d]},forEach:function(a,c){var d=b(this,"keys"),e=b(this,"values");d.forEach(function(b){var d=Ember.guidFor(b);a.call(c,b,e[d])})}});DS.Transaction=Ember.Object.extend({init:function(){c(this,"dirty",{created:g.create(),updated:g.create(),deleted:g.create()})},createRecord:function(a,c){var d=b(this,"store");return d.createRecord(a,c,this)},add:function(a){var d=b(a,"transaction");c(a,"transaction",this)},modelBecameDirty:function(a,c){var d=b(b(this,"dirty"),a),e=c.constructor,g=d.fetch(e);g=g||d.add(e,f.create()),g.add(c)},modelBecameClean:function(a,d){var e=b(b(this,"dirty"),a),f=d.constructor,g=e.fetch(f);g.remove(d),c(d,"transaction",null)},commit:function(){var a=b(this,"dirty"),c=function(c,d,e){var f=b(a,c);f.forEach(function(a,b){if(b.isEmpty())return;b.forEach(function(a){a.willCommit()}),d.call(e,a,b.toArray())})},d={updated:{eachType:function(a,b){c("updated",a,b)}},created:{eachType:function(a,b){c("created",a,b)}},deleted:{eachType:function(a,b){c("deleted",a,b)}}},f=b(this,"store"),g=b(f,"_adapter");if(g&&g.commit)g.commit(f,d);else throw e("Adapter is either null or do not implement `commit` method",this)}})}({}),function(a){var b=Ember.get,c=Ember.set,d=Ember.getPath,e=Ember.String.fmt,f=Ember.Object.extend({init:function(){this.clear()},clear:function(){this.set("presenceSet",{}),this.set("list",Ember.NativeArray.apply([]))},add:function(a){var c=Ember.guidFor(a),d=b(this,"presenceSet"),e=b(this,"list");if(c in d)return;d[c]=!0,e.pushObject(a)},remove:function(a){var c=Ember.guidFor(a),d=b(this,"presenceSet"),e=b(this,"list");delete d[c],e.removeObject(a)},isEmpty:function(){return d(this,"list.length")===0},forEach:function(a,c){b(this,"list").forEach(function(b){a.call(c,b)})}});DS.Store=Ember.Object.extend({init:function(){return(!b(DS,"defaultStore")||b(this,"isDefaultStore"))&&c(DS,"defaultStore",this),c(this,"data",[]),c(this,"_typeMap",{}),c(this,"models",[]),c(this,"modelArrays",[]),c(this,"modelArraysByClientId",{}),c(this,"defaultTransaction",DS.Transaction.create({store:this})),this._super()},transaction:function(){return DS.Transaction.create({store:this})},modelArraysForClientId:function(a){var c=b(this,"modelArraysByClientId"),d=c[a];return d||(d=c[a]=f.create()),d},adapter:null,_adapter:Ember.computed(function(){var a=b(this,"adapter");return typeof a=="string"?d(this,a,!1)||d(window,a):a}).property("adapter").cacheable(),clientIdCounter:-1,createRecord:function(a,e,f){e=e||{};var g=e[d(a,"proto.primaryKey")]||null,h=a.create({data:e||{},store:this,transaction:f});h.adapterDidCreate();var i=this.clientIdToHashMap(a),j=b(this,"models"),k=this.pushHash(e,g,a);return c(h,"clientId",k),j[k]=h,this.updateModelArrays(a,k,e),h},deleteRecord:function(a){a.deleteRecord()},find:function(a,b,c){if(b===undefined)return this.findMany(a,null,null);if(c!==undefined)return this.findMany(a,b,c);if(Ember.typeOf(b)==="object")return this.findQuery(a,b);if(Ember.isArray(b))return this.findMany(a,b);var d=this.clientIdForId(a,b);return this.findByClientId(a,d,b)},findByClientId:function(a,c,d){var f,g=b(this,"models"),h=this.clientIdToHashMap(a);if(c!==undefined)f=g[c],f||(f=this.createModel(a,c),f.setData(h[c]||null));else{c=this.pushHash(null,d,a),f=this.createModel(a,c);var i=b(this,"_adapter");if(i&&i.find)i.find(this,a,d);else throw e("Adapter is either null or does not implement `find` method",this)}return f},findMany:function(a,c,d){var f=this.idToClientIdMap(a),g=this.clientIdToHashMap(a),h,i=Ember.A([]);c?(h=[],c.forEach(function(b){var c=f[b];if(c===undefined||g[c]===undefined)c=this.pushHash(null,b,a),h.push(b);i.push(c)},this)):h=null;if(h&&b(h,"length")>0||d){var j=b(this,"_adapter");if(j&&j.findMany)j.findMany(this,a,h,d);else throw e("Adapter is either null or does not implement `findMany` method",this)}return this.createModelArray(a,i)},findQuery:function(a,c){var d=DS.AdapterPopulatedModelArray.create({type:a,content:Ember.A([]),store:this}),f=b(this,"_adapter");if(f&&f.findQuery)f.findQuery(this,a,c,d);else throw e("Adapter is either null or does not implement `findQuery` method",this);return d},findAll:function(a){var c=this.typeMapFor(a),d=c.findAllCache;if(d)return d;var e=DS.ModelArray.create({type:a,content:Ember.A([]),store:this});this.registerModelArray(e,a);var f=b(this,"_adapter");return f&&f.findAll&&f.findAll(this,a),c.findAllCache=e,e},filter:function(a,b){var c=DS.FilteredModelArray.create({type:a,content:Ember.A([]),store:this,filterFunction:b});return this.registerModelArray(c,a,b),c},hashWasUpdated:function(a,b){var c=this.clientIdToHashMap(a),d=c[b];this.updateModelArrays(a,b,d)},commit:function(){b(this,"defaultTransaction").commit()},didUpdateRecords:function(a,b){arguments.length===2?a.forEach(function(a,c){this.didUpdateRecord(a,b[c])},this):a.forEach(function(a){this.didUpdateRecord(a)},this)},didUpdateRecord:function(a,c){if(arguments.length===2){var d=b(a,"clientId"),e=this.clientIdToHashMap(a.constructor);e[d]=c,a.set("data",c)}a.adapterDidUpdate()},didDeleteRecords:function(a){a.forEach(function(a){a.adapterDidDelete()})},didDeleteRecord:function(a){a.adapterDidDelete()},didCreateRecords:function(a,e,f){var g,h,i=d(a,"proto.primaryKey"),j=this.idToClientIdMap(a),k=this.clientIdToHashMap(a),l=this.idList(a);for(var m=0,n=b(e,"length");m