Files
reactfire/dist/reactfire.min.js
2014-09-04 06:21:10 +00:00

10 lines
2.1 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.2.0
* https://github.com/firebase/reactfire/
* License: MIT
*/
var ReactFireMixin=function(){"use strict";var e={componentWillMount:function(){this.firebaseRefs={},this.firebaseListeners={}},componentWillUnmount:function(){for(var e in this.firebaseRefs)this.firebaseRefs.hasOwnProperty(e)&&this.unbind(e)},bindAsArray:function(e,t){this._bind(e,t,!0)},bindAsObject:function(e,t){this._bind(e,t,!1)},_bind:function(e,t,i){this._validateBindVar(t);var n;if("undefined"==typeof e.ref||e.ref()instanceof Firebase==!1?n="firebaseRef must be an instance of Firebase":"boolean"!=typeof i&&(n="bindAsArray must be a boolean. Got: "+i),"undefined"!=typeof n)throw new Error("ReactFire: "+n);this.firebaseRefs[t]=e.ref(),this.firebaseListeners[t]=e.on("value",function(e){var n={};n[t]=i?this._toArray(e.val()):e.val(),this.setState(n)}.bind(this))},unbind:function(e){if(this._validateBindVar(e),"undefined"==typeof this.firebaseRefs[e])throw new Error('unexpected value for bindVar. "'+e+'" was either never bound or has already been unbound');this.firebaseRefs[e].off("value",this.firebaseListeners[e]),delete this.firebaseRefs[e],delete this.firebaseListeners[e]},_validateBindVar:function(e){var t;if("string"!=typeof e?t="bindVar must be a string. Got: "+e:0===e.length?t='bindVar must be a non-empty string. Got: ""':e.length>768?t="bindVar is too long to be stored in Firebase. Got: "+e:/[\[\].#$\/\u0000-\u001F\u007F]/.test(e)&&(t="bindVar cannot contain any of the following characters: . # $ ] [ /. Got: "+e),"undefined"!=typeof t)throw new Error("ReactFire: "+t)},_isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},_toArray:function(e){var t=[];if(e)if(this._isArray(e))t=e;else if("object"==typeof e)for(var i in e)e.hasOwnProperty(i)&&t.push(e[i]);return t}};return e}();"undefined"!=typeof module&&"undefined"!=typeof process&&(module.exports=ReactFireMixin);