Skip to content

Instantly share code, notes, and snippets.

@fernandozamoraj
Last active August 25, 2017 20:00
Show Gist options
  • Save fernandozamoraj/d6e35b5e80cd6c6726e6b1802bfa8c8d to your computer and use it in GitHub Desktop.
Save fernandozamoraj/d6e35b5e80cd6c6726e6b1802bfa8c8d to your computer and use it in GitHub Desktop.
FirebaseQuickSample
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {}
$.getJSON("config.json", function(data){
config = data
console.log(config)
firebase.initializeApp(config);
setup()
})
</script>
<title>Document</title>
</head>
<!--
This demo can only be run on a server... you cannot run this file directly from your browser
A good server is live-server
just run the following command
npm install -g live-server
-->
<input type="text" placeholder="name" id="name">
<input type="email" placeholder="email" id="email">
<button id="submit-btn">submit</button>
<ul id="members">
</ul>
<body>
</body>
<script>
/*
userRef.on("child_added", function(snap){
console.log(snap.val())
var user = snap.val()
var ch =document.createElement("LI")
ch.innerHTML = user.EMail + " " + user.Name
members.appendChild(ch)
})
*/
function setup(){
var database = firebase.database();
var userRef = database.ref("myusers")
var submitBtn = document.getElementById("submit-btn")
var members =document.getElementById("members")
userRef.on("value", function(snap){
members.innerHTML = ""
snap.forEach(function(childSnap) {
var user = childSnap.val()
var ch =document.createElement("LI")
ch.innerHTML = user.EMail + " " + user.Name + " " + childSnap.key
members.appendChild(ch)
});
})
submitBtn.addEventListener("click", function(e){
var name =getTextValue("name")
var email =getTextValue("email")
userRef.push({EMail: email, Name: name})
})
}
function getTextValue(inputId){
var element =document.getElementById(inputId)
return element.value
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment