/*! matchbox - v0.0.0 - 2014-08-18 * https://github.com/gpbmike/matchbox * Copyright (c) 2014 Mike Horn; Licensed MIT */ !function(){"use strict";DS.FirebaseSerializer=DS.JSONSerializer.extend(),DS.FirebaseAdapter=DS.Adapter.extend({init:function(){if(!this.firebase||"object"!=typeof this.firebase)throw new Error("Please set the `firebase` property on the adapter.");this._ref=this.firebase.ref(),this._findAllMapForType={},this._findQueryMapForType={}},generateIdForRecord:function(){return this._ref.push().name()},_assignIdToPayload:function(a){var b=a.val();return null!==b&&"object"==typeof b&&"undefined"==typeof b.id&&(b.id=a.name()),b},find:function(a,b,c){var d=this,e=!1,f=this._getRef(b,c),g=a.serializerFor(b);return new Ember.RSVP.Promise(function(h,i){f.on("value",function(j){var k=d._assignIdToPayload(j),l=a.getById(b,j.name());e?null===k&&l&&!l.get("isDeleted")?Ember.run(function(){l.destroyRecord()}):null!==k&&Ember.run(function(){l.setProperties(g.extractSingle(a,b,k)).save()}):(e=!0,null===k?(a.hasRecordForId(b,c)&&Ember.run(function(){a.dematerializeRecord(l)}),Ember.run(null,i,"no record was found at %@"+f.toString())):Ember.run(null,h,k))},function(a){e||Ember.run(null,i,a)})},"DS: FirebaseAdapter#find "+b+" to "+f.toString())},findAll:function(a,b){function c(c){if(f){var d=c.val();d.id=c.name(),a.push(b,h.extractSingle(a,b,d))}}function d(c){var d=a.getById(b,c.name());f&&d&&!d.get("isDeleted")&&d.setProperties(h.extractSingle(a,b,c.val())).save()}function e(c){var d=a.getById(b,c.name());f&&d&&!d.get("isDeleted")&&d.destroyRecord()}var f=!1,g=this._getRef(b),h=a.serializerFor(b),i=this;return new Ember.RSVP.Promise(function(a,h){function j(a){f||(f=!0,Ember.run(null,h,a))}function k(b){f=!0;var c=Ember.A([]);b.forEach(function(a){var b=a.val();b.id=a.name(),c.push(b)}),Ember.run(null,a,c)}Ember.isNone(i._findAllMapForType[b])&&(i._findAllMapForType[b]=!0,g.on("child_added",c,j),g.on("child_changed",d,j),g.on("child_removed",e,j)),g.once("value",k,j)},"DS: FirebaseAdapter#findAll "+b+" to "+g.toString())},findQuery:function(a,b,c){function d(c){if(f){var d=c.val();d.id=c.name(),a.push(b,h.extractSingle(a,b,d))}}function e(c){f&&a.hasRecordForId(b,c.name())&&a.getById(b,c.name()).setProperties(h.extractSingle(a,b,c.val())).save()}var f=!1,g=this._getRef(b),h=a.serializerFor(b),i=this;return c=c||{},c.limit&&(g=g.limit(c.limit)),c.startAt&&(g=c.startAt===!0?g.startAt():g.startAt(c.startAt)),c.endAt&&(g=c.endAt===!0?g.endAt():g.endAt(c.endAt)),new Ember.RSVP.Promise(function(a,h){function j(a){f||(f=!0,Ember.run(null,h,a))}function k(b){f=!0;var c=Ember.A([]);b.forEach(function(a){var b=a.val();b.id=a.name(),c.push(b)}),Ember.run(null,a,c)}var l=b;Object.keys(c).forEach(function(a){l=l+a+c[a]}),Ember.isNone(i._findQueryMapForType[l])&&(i._findQueryMapForType[l]=!0,g.on("child_added",d,j),g.on("child_changed",e,j)),g.once("value",k,j)},"DS: FirebaseAdapter#findAll "+b+" to "+g.toString())},createRecord:function(a,b,c){var d=c.serialize({includeId:!1}),e=this._getRef(b,c.id);return new Ember.RSVP.Promise(function(a,b){e.set(JSON.parse(JSON.stringify(d)),function(c){c?b(c):a()})},"DS: FirebaseAdapter#createRecord "+b+" to "+e.toString())},updateRecord:function(){return this.createRecord.apply(this,arguments)},deleteRecord:function(a,b,c){var d=this._getRef(b,c.id);return new Ember.RSVP.Promise(function(a,b){d.remove(function(c){c?Ember.run(null,b,c):Ember.run(null,a)})},"DS: FirebaseAdapter#deleteRecord "+b+" to "+d.toString())},pathForType:function(a){var b=Ember.String.camelize(a);return Ember.String.pluralize(b)},_getRef:function(a,b){var c=this._ref;return a&&(c=c.child(this.pathForType(a.typeKey))),b&&(c=c.child(b)),c},_findAllMapForType:void 0,_findQueryMapForType:void 0})}();