Added ReactFire to bower and now reference it from todoApp example. Cleaned up todoApp example directory.

This commit is contained in:
jacobawenger
2014-04-30 19:16:48 -07:00
parent 9c7862c29e
commit afc529856b
5 changed files with 7 additions and 106 deletions

View File

@@ -1,101 +0,0 @@
var ReactFireMixin = {
/********************/
/* MIXIN LIFETIME */
/********************/
/* Initializes the Firebase binding refs array */
componentWillMount: function() {
this.firebaseRefs = {};
},
/* Removes any remaining Firebase bindings */
componentWillUnmount: function() {
for (var key in this.firebaseRefs) {
this.unbind(key);
};
},
/*************/
/* BINDING */
/*************/
/* Creates a binding between Firebase and the inputted bind variable as an array */
bindAsArray: function(firebaseRef, bindVar) {
this._bind(firebaseRef, bindVar, true);
},
/* Creates a binding between Firebase and the inputted bind variable as an object */
bindAsObject: function(firebaseRef, bindVar) {
this._bind(firebaseRef, bindVar, false);
},
/* Creates a binding between Firebase and the inputted bind variable as either an array or object */
_bind: function(firebaseRef, bindVar, bindAsArray) {
this.firebaseRefs[bindVar] = firebaseRef;
firebaseRef.on("value", function(dataSnapshot) {
var newState = {};
if (bindAsArray) {
newState[bindVar] = this._toArray(dataSnapshot.val());
}
else {
newState[bindVar] = dataSnapshot.val();
}
this.setState(newState);
}.bind(this));
},
/* Removes the binding between Firebase and the inputted bind variable */
unbind: function(bindVar) {
this.firebaseRefs[bindVar].off("value");
delete this.firebaseRefs[bindVar];
},
/*************/
/* HELPERS */
/*************/
/* Returns true if the inputted object is a number */
_isNumeric: function(obj) {
try {
return (((obj - 0) == obj) && (obj.length > 0));
} catch (e) {
return false;
} // try
}, // isNumeric()
/* Returns true if the inputted object is a JavaScript array */
_isArray: function(obj) {
if (!obj) { return false; }
try {
if (!(obj.propertyIsEnumerable("length"))
&& (typeof obj === "object")
&& (typeof obj.length === "number")) {
for (var idx in obj) {
if (!this._isNumeric(idx)) { return false; }
} // for (var idx in object)
return true;
} else {
return false;
} // if (!(obj.propertyIsEnumerable("length"))...
} catch (e) {
return false;
} // try
}, // isArray()
/* Converts a Firebase list to a JavaScript array */
_toArray: function(list) {
var k, out = [];
if (list) {
if (this._isArray(list)) {
out = list;
}
else if (typeof(list) === "object") {
for (k in list) {
if (list.hasOwnProperty(k)) {
out.push(list[k]);
}
}
}
}
return out;
}
};

View File

@@ -22,6 +22,7 @@
],
"dependencies": {
"react": "~0.10.0",
"firebase": "~1.0.12"
"firebase": "~1.0.12",
"ReactFire": "~0.1.0"
}
}

View File

@@ -11,7 +11,7 @@
<script src="bower_components/firebase/firebase.js"></script>
<!-- ReactFireMixin -->
<script src="ReactFireMixin.js"></script>
<script src="bower_components/ReactFire/ReactFireMixin.js"></script>
<!-- Custom JS -->
<script type="text/jsx" src="js/todoAppOriginal.js"></script>