Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase DB to Static File for React JS
// first you have included your FireBase client JS
var leaguesNode = new Firebase("https://XXXX.firebaseio.com/Leagues");
jsonLeagues = '{"leagues": []}';
leaguesNode.once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key();
var val = childSnapshot.val();
var obj = JSON.parse(jsonLeagues);
obj['leagues'].push({"id":val,"content":key});
jsonLeagues = JSON.stringify(obj);
});
$.ajax({
type: "POST",
url: "save.php",
data: {
leaguePost: "var LeagueArray = "+jsonLeagues+";",
}
});
--- SAVE.php to 2015-12-14-13-golazoDb.js ---
<?php
setlocale(LC_TIME, "us_EN");
date_default_timezone_set("America/New_York");
$date = strftime("%Y-%m-%d-%H");
$file = "data_dumps/".$date."-golazoDb.js";
$f = fopen ($file, 'w');
fwrite($f, $_POST["leaguePost"]);
fclose($f);
?>
});
---- React Component ----
var LeagueList = React.createClass({
render: function () {
var nodes = this.props.LeagueArray.leagues;
return (
<div className="node-list" id="LeagueList">
{
nodes.map(function (node) {
var title = node.content.substring(0,
node.content.indexOf('\n')
);
title = title || node.content;
return (
<div key={node.id} className="node-summary">{title}</div>
);
})
}
</div>
);
}
});
ReactDOM.render(
<LeagueList LeagueArray={LeagueArray}/>,
golazoApp
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.