Created
May 6, 2015 18:52
-
-
Save alastairparagas/b3a80f8aba719599dfda to your computer and use it in GitHub Desktop.
Listing elements of a Firebase node to a page
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
(function (window) { | |
'use strict'; | |
var FirebaseRef = new window.Firebase("firebase-url-here"), | |
document = window.document; | |
FirebaseRef.on("value", function (eventsSnapshot) { | |
var events = eventsSnapshot.val(), | |
eventsListed = {}, | |
eventsDateSorted = [], | |
eventsDateSortedIterator = 0, | |
eventsDateSortedLength = 0, | |
currentEvent, | |
event, | |
eventDate; | |
// Put each event listing in the Events Firebase Node to an array keyed with event's timestamp (for sorting) | |
for (event in events) { | |
currentEvent = events[event]; | |
if (events.hasOwnProperty(event) && currentEvent.date !== undefined) { | |
// If there is already an event with same key (timestamps are used as keys), increase | |
// the timestamp by 1. | |
if (eventsListed.hasOwnProperty(currentEvent.date.starts)) { | |
eventsListed[currentEvent.date.starts + 1] = currentEvent; | |
} else { | |
eventsListed[currentEvent.date.starts] = currentEvent; | |
} | |
} | |
} | |
// Sort events, oldest event being the first, | |
eventsDateSorted = Object.keys(eventsListed).sort(function (a, b) { | |
return a - b; | |
}); | |
// List the sorted events | |
// Cache the length of eventsDateSorted array (performance) | |
for (eventsDateSortedLength = eventsDateSorted.length; | |
eventsDateSortedIterator < eventsDateSortedLength; | |
eventsDateSortedIterator + 1) { | |
event = eventsListed[eventsDateSorted[eventsDateSortedIterator]]; | |
eventDate = new Date(event.date.starts); | |
var trNode = document.createElement("tr"), | |
tdNameNode = document.createElement("td"), | |
tdNameTextNode = document.createTextNode(event.name), | |
tdDateNode = document.createElement("td"), | |
tdDateTextNode = document.createTextNode(eventDate.toLocaleString()), | |
tdLocationNode = document.createElement("td"), | |
tdLocationTextNode = document.createTextNode(event.location.name), | |
tdTypeNode = document.createElement("td"), | |
tdTypeTextNode = document.createTextNode(event.types && typeof event.types === "object" ? event.types.join(", ") : " "); | |
// Construct the DOM Node Tree | |
tdNameNode.appendChild(tdNameTextNode); | |
tdDateNode.appendChild(tdDateTextNode); | |
tdLocationNode.appendChild(tdLocationTextNode); | |
tdTypeNode.appendChild(tdTypeTextNode); | |
trNode.appendChild(tdNameNode); | |
trNode.appendChild(tdDateNode); | |
trNode.appendChild(tdLocationNode); | |
trNode.appendChild(tdTypeNode); | |
document.getElementById("eventsList").appendChild(trNode); | |
} | |
}); | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment