-
Notifications
You must be signed in to change notification settings - Fork 10
/
firebase-as-array.min.js
4 lines (4 loc) · 3.08 KB
/
firebase-as-array.min.js
1
2
3
4
/*! Firebase.getAsArray - v0.1.0 - 2014-04-21
* Copyright (c) 2014 Kato
* MIT LICENSE */
!function(a){function b(a,b){this.list=[],this.subs=[],this.ref=a,this.eventCallback=b,this._wrapList(),this._initListeners()}function c(a,b){if(d(a)&&d(b)){var c;for(c in a)"$id"!==c&&a.hasOwnProperty(c)&&!b.hasOwnProperty(c)&&delete a[c];for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}return b}function d(a){return"object"==typeof a&&null!==a}function e(a,b){for(var c=0,d=a.length;d>c;c++)if(a[c].$id===b)return c;return-1}function f(a){return a&&"object"==typeof a&&(delete a.$id,a.hasOwnProperty(".value")&&(a=a[".value"])),void 0===a&&(a=null),a}function g(a,b){return"object"==typeof b&&b||(b={".value":b}),b.$id=a,b}a.getAsArray=function(a,c){return new b(a,c).getList()},b.prototype={getList:function(){return this.list},add:function(a){var b=this.ref.push().name(),c=this.ref.child(b);return arguments.length>0&&c.set(f(a),this._handleErrors.bind(this,b)),c},set:function(a,b){this.ref.child(a).set(f(b),this._handleErrors.bind(this,a))},update:function(a,b){this.ref.child(a).update(f(b),this._handleErrors.bind(this,a))},setPriority:function(a,b){this.ref.child(a).setPriority(b)},remove:function(a){this.ref.child(a).remove(this._handleErrors.bind(null,a))},posByKey:function(a){return e(this.list,a)},placeRecord:function(a,b){if(null===b)return 0;var c=this.posByKey(b);return-1===c?this.list.length:c+1},getRecord:function(a){var b=this.posByKey(a);return-1===b?null:this.list[b]},dispose:function(){var a=this.ref;this.subs.forEach(function(b){a.off(b[0],b[1])}),this.subs=[]},_serverAdd:function(a,b){var c=g(a.name(),a.val());this._moveTo(a.name(),c,b),this._handleEvent("child_added",a.name(),c)},_serverRemove:function(a){var b=this.posByKey(a.name());-1!==b&&(this.list.splice(b,1),this._handleEvent("child_removed",a.name(),this.list[b]))},_serverChange:function(a){var b=this.posByKey(a.name());-1!==b&&(this.list[b]=c(this.list[b],g(a.name(),a.val())),this._handleEvent("child_changed",a.name(),this.list[b]))},_serverMove:function(a,b){var c=a.name(),d=this.posByKey(c);if(-1!==d){var e=this.list[d];this.list.splice(d,1),this._moveTo(c,e,b),this._handleEvent("child_moved",a.name(),e)}},_moveTo:function(a,b,c){var d=this.placeRecord(a,c);this.list.splice(d,0,b)},_handleErrors:function(a,b){b&&(this._handleEvent("error",null,a),console.error(b))},_handleEvent:function(a,b,c){this.eventCallback&&this.eventCallback(a,b,c)},_wrapList:function(){this.list.$indexOf=this.posByKey.bind(this),this.list.$add=this.add.bind(this),this.list.$remove=this.remove.bind(this),this.list.$set=this.set.bind(this),this.list.$update=this.update.bind(this),this.list.$move=this.setPriority.bind(this),this.list.$rawData=function(a){return f(this.getRecord(a))}.bind(this),this.list.$off=this.dispose.bind(this)},_initListeners:function(){this._monit("child_added",this._serverAdd),this._monit("child_removed",this._serverRemove),this._monit("child_changed",this._serverChange),this._monit("child_moved",this._serverMove)},_monit:function(a,b){this.subs.push([a,this.ref.on(a,b.bind(this))])}}}("undefined"==typeof window?exports:window.Firebase);