Last active
December 25, 2015 03:59
-
-
Save kingcody/6913841 to your computer and use it in GitHub Desktop.
Active Classing Patch to routes/index.js
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
unchanged: | |
--- b/routes/index.js | |
+++ b/routes/index.js | |
@@ -4,6 +4,13 @@ | |
var merge = require('merge'), | |
fs = require('fs'); | |
+// Returns a camel cased version of input | |
+var camelCase = function camelCase(input) { | |
+ return input.toLowerCase().replace(/-(.)/g, function(match, group1) { | |
+ return group1.toUpperCase(); | |
+ }); | |
+}; | |
+ | |
// Router function that gets exported to the main(app.js) loop. | |
exports.router = function(req, res, next){ | |
// Strip leading slashes, .stuff's and lower casing the string. | |
@@ -31,10 +38,17 @@ | |
// Data routing function. | |
var dataRouter = function dataRouter(page) { | |
+ // Set pageName to the actual jade page requested, camelCased | |
+ var pageName = camelCase(page.split('/')[page.split('/').length - 1]), | |
// Fetch most recent siteData | |
- var siteData = fetchJSON(__dirname + '/../data/siteData.json'); | |
- // Merge the global and page data together, then return it. | |
- return merge(siteData.globalData, siteData.pageData[page]); | |
+ siteData = fetchJSON(__dirname + '/../data/siteData.json'), | |
+ // Merge the global and page data together. | |
+ pageData = merge(siteData.globalData, siteData.pageData[page]); | |
+ // Set the pageName variable to 'active'. Can be used for classes | |
+ pageData[pageName] = 'active'; | |
+ | |
+ // Return the processed pageData | |
+ return pageData; | |
}; | |
// Fetch JSON data function | |
diff -u b/routes/index.js b/routes/index.js | |
--- b/routes/index.js | |
+++ b/routes/index.js | |
@@ -39,13 +39,14 @@ | |
// Data routing function. | |
var dataRouter = function dataRouter(page) { | |
// Set pageName to the actual jade page requested, camelCased | |
- var pageName = camelCase(page.split('/')[page.split('/').length - 1]), | |
+ var pageArray = page.split('/'), | |
+ pageName = camelCase(pageArray[pageArray.length - 1]), | |
// Fetch most recent siteData | |
siteData = fetchJSON(__dirname + '/../data/siteData.json'), | |
// Merge the global and page data together. | |
pageData = merge(siteData.globalData, siteData.pageData[page]); | |
- // Set the pageName variable to 'active'. Can be used for classes | |
- pageData[pageName] = 'active'; | |
+ // Set the pageName variable to activeClass. Can be used for classes | |
+ pageData[pageName] = pageData.activeClass; | |
// Return the processed pageData | |
return pageData; | |
only in patch2: | |
unchanged: | |
--- a/data/siteData.json | |
+++ b/data/siteData.json | |
@@ -1,6 +1,7 @@ | |
{ | |
"globalData": { | |
- "title": "Asian Bistro" | |
+ "title": "Asian Bistro", | |
+ "activeClass": "active" | |
}, | |
"pageData": { | |
"index": {}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment