#hof-behaviour-session
HOF Behaviour for reading and writing to the session
##Usage
###With mixwith.js
const mix = require('mixwith').mix;
const Session = require('hof-behaviour-session');
const BaseController = require('hof-form-controller');
class MyController extends mix(BaseController).with(Session) {
...
}
MyController
now extends hof-form-controller
and has hof-behaviour-session
functionality mixed in.
##Functionality
This mixin extends hof-form-controller
by persisting the form data to the sessionModel
- assuming the session-model middleware has been applied.
The following form controller methods are used:
getValues(req, res, cb)
- calls callback withnull
and a map of all items in thesessionModel
, extended witherrorValues
- to persist entered values on current step if validation failssaveValues(req, res, cb)
- Called on success. Sets all step fields inreq.form.values
to the sessionModel, unsetserrorValues
.getErrors(req)
- returns all errors for fields on the current step (req.form.options.fields
), excluding redirects. Set toreq.form.errors
inhof-form-controller
.setErrors(err, req)
- called on validation error(s). Sets the current step field values aserrorValues
in sessionModel to be used ingetValues
. Setserrors
to sessionModel - a map offield-name: error
to be used ingetErrors
.locals(req, res)
- Extends the result ofsuper.locals
withbaseUrl
(req.baseUrl
) andnextPage
(the result ofthis.getNextStep(req, res)
).missingPrereqHandler(req, res)
- Error handler called when aMISSING_PREREQ
error is thrown from the check-progress middleware. This occurs if a step is visited out of sequence. This error handler causes the user to be redirected to the last completed step, or the first step if none have been completed.errorHandler(err, req, res, next)
- checks iferr.code
isMISSING_PREREQ
, if so callsmissingPrereqHandler
, if not callssuper
to hand over to parent error handler.