Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dubrod
Created December 14, 2015 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubrod/3e6276350bd5ee27f232 to your computer and use it in GitHub Desktop.
Save dubrod/3e6276350bd5ee27f232 to your computer and use it in GitHub Desktop.
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