Skip to content

Commit

Permalink
refactor checked function
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanman committed Apr 3, 2017
1 parent 2d5c201 commit 6bfd69d
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ if (env === 'production' && useHttps === 'true') {
app.use(utils.forceHttps)
}

if (useAutoStoreData === 'true') {
app.use(utils.autoStoreData)
var addCheckedFunction = function (app, nunjucksEnv) {
app.use(function (req, res, next) {
// add nunjucks function to get values, needs to be here as they need access to req.session
// add nunjucks function called 'checked' to populate radios and checkboxes,
// needs to be here as it needs access to req.session and nunjucks environment

nunjucksAppEnv.addGlobal('checked', function (name, value) {
nunjucksEnv.addGlobal('checked', function (name, value) {
if (req.session.data === undefined) {
return ''
}
Expand All @@ -144,31 +144,12 @@ if (useAutoStoreData === 'true') {

next()
})
}

documentationApp.use(function (req, res, next) {
// add nunjucks function to get values, needs to be here as they need access to req.session

nunjucksDocumentationEnv.addGlobal('checked', function (name, value) {
if (req.session.data === undefined) {
return ''
}

var storedValue = req.session.data[name]

if (storedValue === undefined) {
return ''
}

if (Array.isArray(storedValue)) {
var inArray = storedValue.indexOf(value) !== -1
return inArray ? 'checked' : ''
} else {
return value === storedValue ? 'checked' : ''
}
})

next()
})
if (useAutoStoreData === 'true') {
app.use(utils.autoStoreData)
addCheckedFunction(app, nunjucksAppEnv)
addCheckedFunction(documentationApp, nunjucksDocumentationEnv)
}

// Disallow search index idexing
Expand Down

0 comments on commit 6bfd69d

Please sign in to comment.