Skip to content

Instantly share code, notes, and snippets.

@tysonchamp
Created August 4, 2016 02:44
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 tysonchamp/a7097284b3278a23e2a939c1ca08af2a to your computer and use it in GitHub Desktop.
Save tysonchamp/a7097284b3278a23e2a939c1ca08af2a to your computer and use it in GitHub Desktop.
json code by ezarya
/*
var lightBoxData = {};
JSONData.forEach(function (item) { // loop through each item in the data set
lightBoxData[item.id] = item; // make each item referencable by the ID data field
});
*/
var route = {};
var templateFolder = "templates/";
$(document).ready(function () {
console.log("document.ready fired");
$.getJSON("json/info.json", saveJSONData);
$(window).on('hashchange', router);
$(window).on('load', router);
$(window).trigger('hashchange');
});
function saveJSONData(JSONData) { // request JSONData from the server
console.debug("SaveJSONData Recieved JSON Data: %O", JSONData); // loop through each item in the data set
console.debug(JSONData[0]["title"]);
registerRoute(["#","home","/"], "home.html", "#view",JSONData);
registerRoute(["gallery"], "gallery.html", "#view",JSONData);
registerRoute(["contact"], "contact.html", "#view",JSONData);
registerRoute(["*"], "error.html", "#view",JSONData);
}
//-----------------------------------------------------------------------------
// set up associations between hashURLS and templates
//-----------------------------------------------------------------------------
function registerRoute(hashList, templateFilename, ContainerID, data) {
var path = templateFolder + templateFilename;
$.get(path, function (rawTemplateHTML) {
var compiledTemplateJS = Handlebars.compile(rawTemplateHTML);
hashList.forEach(function(hash) { // loop through each item in the data set
route[hash] = {
template: compiledTemplateJS(data),
ID: ContainerID,
};
});
console.debug("registerRoute added routes %O", route);
console.debug("got the data", data);
$(window).trigger('hashchange');
});
}
//-----------------------------------------------------------------------------
// handle hashchange event and display a template
//-----------------------------------------------------------------------------
function router() {
var url = location.hash.slice(1) || "/";
var target = route[url];
console.debug("URL",url);
console.debug("router routing to",target);
if(target){
console.debug($(target.ID).html);
console.debug("router linked to target %O", target);
$(target.ID).html(target.template); // dump template into empty UL on page
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment