Last active
August 29, 2015 14:17
-
-
Save pankajpatel/5e7255dbe44b6bde8fb2 to your computer and use it in GitHub Desktop.
Contacts Store Application JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create firebase reference | |
var dbRef = new Firebase("https://contactb.firebaseio.com/"); | |
var contactsRef = dbRef.child('contacts') | |
//load older conatcts as well as any newly added one... | |
contactsRef.on("child_added", function(snap) { | |
console.log("added", snap.key(), snap.val()); | |
document | |
.querySelector('#contacts') | |
.innerHTML += contactHtmlFromObject(snap.val()); | |
}); | |
//save contact | |
document | |
.querySelector('.addValue') | |
.addEventListener("click", function( event ) { | |
event.preventDefault(); | |
if( document.querySelector('#name').value != '' | |
|| document.querySelector('#email').value != '' ){ | |
contactsRef | |
.push({ | |
name: document.querySelector('#name').value, | |
email: document.querySelector('#email').value, | |
location: { | |
city: document.querySelector('#city').value, | |
state: document.querySelector('#state').value, | |
zip: document.querySelector('#zip').value | |
} | |
}) | |
contactForm.reset(); | |
} else { | |
alert('Please fill atlease name or email!'); | |
} | |
}, false); | |
//prepare conatct object's HTML | |
function contactHtmlFromObject(contact){ | |
console.log( contact ); | |
var html = ''; | |
html += '<li class="list-group-item contact">'; | |
html += '<div>'; | |
html += '<p class="lead">'+contact.name+'</p>'; | |
html += '<p>'+contact.email+'</p>'; | |
html += '<p><small title="' | |
+contact.location.zip+'">' | |
+contact.location.city | |
+', ' | |
+contact.location.state | |
+'</small></p>'; | |
html += '</div>'; | |
html += '</li>'; | |
return html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment