Files
reactfire/dist/reactfire.min.js
2015-07-16 18:33:43 +00:00

10 lines
2.9 KiB
JavaScript

/*!
* ReactFire is an open-source JavaScript library that allows you to add a
* realtime data source to your React apps by providing and easy way to let
* Firebase populate the state of React components.
*
* ReactFire 0.5.0
* https://github.com/firebase/reactfire/
* License: MIT
*/
!function(e,t){"use strict";"function"==typeof define&&define.amd?define([],function(){return e.ReactFireMixin=t()}):"object"==typeof exports?module.exports=t():e.ReactFireMixin=t()}(this,function(){"use strict";function e(e,t){for(var i=0,n=e.length;n>i;++i)if(e[i][".key"]===t)return i;return-1}function t(e){throw new Error("ReactFire: "+e)}function i(e){var i;"string"!=typeof e?i="Bind variable must be a string. Got: "+e:0===e.length?i='Bind variable must be a non-empty string. Got: ""':e.length>768?i="Bind variable is too long to be stored in Firebase. Got: "+e:/[\[\].#$\/\u0000-\u001F\u007F]/.test(e)&&(i="Bind variable cannot contain any of the following characters: . # $ ] [ /. Got: "+e),"undefined"!=typeof i&&t(i)}function n(e,t){var i={};return"object"==typeof t&&null!==t?i=t:i[".value"]=t,i[".key"]=e,i}function s(e,t){var i=t.key(),s=t.val();this.data[e]=n(i,s),this.setState(this.data)}function a(t,i,s){var a,r=i.key(),o=i.val(),f=this.data[t];if(null===s)a=0;else{var d=e(f,s);a=d+1}f.splice(a,0,n(r,o)),this.setState(this.data)}function r(t,i){var n=this.data[t],s=e(n,i.key());n.splice(s,1),this.setState(this.data)}function o(t,i){var s=i.key(),a=i.val(),r=this.data[t],o=e(r,s);r[o]=n(s,a),this.setState(this.data)}function f(t,i,n){var s,a=i.key(),r=this.data[t],o=e(r,a),f=r.splice(o,1)[0];if(null===n)s=0;else{var d=e(r,n);s=d+1}r.splice(s,0,f),this.setState(this.data)}function d(e,n,d,h){"[object Object]"!==Object.prototype.toString.call(e)&&t("Invalid Firebase reference"),i(n),"undefined"!=typeof this.firebaseRefs[n]&&t("this.state."+n+" is already bound to a Firebase reference"),this.firebaseRefs[n]=e.ref(),h?(this.data[n]=[],this.setState(this.data),this.firebaseListeners[n]={child_added:e.on("child_added",a.bind(this,n),d),child_removed:e.on("child_removed",r.bind(this,n),d),child_changed:e.on("child_changed",o.bind(this,n),d),child_moved:e.on("child_moved",f.bind(this,n),d)}):this.firebaseListeners[n]={value:e.on("value",s.bind(this,n),d)}}var h={componentWillMount:function(){this.data={},this.firebaseRefs={},this.firebaseListeners={}},componentWillUnmount:function(){for(var e in this.firebaseRefs)this.firebaseRefs.hasOwnProperty(e)&&this.unbind(e)},bindAsArray:function(e,t,i){var n=d.bind(this);n(e,t,i,!0)},bindAsObject:function(e,t,i){var n=d.bind(this);n(e,t,i,!1)},unbind:function(e,n){i(e),"undefined"==typeof this.firebaseRefs[e]&&t("this.state."+e+" is not bound to a Firebase reference");for(var s in this.firebaseListeners[e])if(this.firebaseListeners[e].hasOwnProperty(s)){var a=this.firebaseListeners[e][s];this.firebaseRefs[e].off(s,a)}this.firebaseRefs[e]=void 0,this.firebaseListeners[e]=void 0;var r={};r[e]=void 0,this.setState(r,n)}};return h});