JrResult

helpers/jrresult~ JrResult

new JrResult()

Source:

We use JrResult object instances to store the results of operations, where we may want to indicate a success or an error with additional information about the nature of the error. The result can hold multiple errors, possibly keyed to fields (for example an error message corresponding to each input form variable)

Methods

(static) _unusedCodeExpressMiddlewareInjectSessionResult(options)

Source:

Experimental express middleware function that automatically grabs any pending jrResult error from session and makes it available in the locals variable of a view for display

Notes

express middleware helper the idea here is we want to take any session jrResult found in session, and put it automatically in any render call res all this does is save us from having to make every render look like this: res.render("viewpage", { jrResult: JrResult.restoreFromSession(req); }); see https://stackoverflow.com/questions/9285880/node-js-express-js-how-to-override-intercept-res-render-function

ATTN: 5/27/19 -- although this worked flawlessly, we have decided to force the manaul use of this into all render calls, to have better control over it but the proper call now is a bit more involved, it should be like this: res.render("urlpath", { jrResult: jrContext.mergeSessionMessages(), // or if we have no result of our own: jrResult: jrContext.mergeSessionMessages() } this old code does NOT do a merge combine of session data with manual jrresult, so can no longer be used

Parameters:
Name Type Description
options *

(static) isBlank(obj)

Source:

Simple helper function thats lets us easily check for a case where no errors or messages were added to a JrResult (i.e. it can be completely ignored)

Parameters:
Name Type Description
obj object
Returns:

true if the JrResult obj has no data

(static) isJrResult(obj)

Source:

Check if the passed value is a JrResult

Parameters:
Name Type Description
obj *
Returns:

true if obj is an instance of JrResult

(static) makeClone(source)

Source:

make a clone of an existing jr result

Parameters:
Name Type Description
source object
Returns:

new JrResult object

(static) makeError()

Source:

Helper function to create a new JrResult object and store an error for it

Returns:

new JrResult object

(static) makeMessage()

Source:

Helper function to create a new JrResult object and store a generic message in it

Returns:

new JrResult object

(static) makeNew()

Source:

Helper function to create a new blank JrResult object

Returns:

new JrResult object

(static) makeSuccess()

Source:

Helper function to create a new JrResult object and store a success message in it

Returns:

new JrResult object

(static) passportOrJrResultErrorAsString(info)

Source:

Return the string associated with a passport error, OR convert the JrResult to an error string if they pass in a JrResult

Parameters:
Name Type Description
info object

the passport info error (or a jrResult)

Returns:

error message string

(static) retrieveThenRemoveFromSession(req)

Source:

Create a result from the session, or return undefined if there is none, then clear session jrResult

Notes
  • Important: The results will be REMOVED from the session after this is called
Parameters:
Name Type Description
req *

express request

Returns:

jrResult from session or undefined if none found

(static) undefinedIfBlank()

Source:

Simple helper function thats makes it easier to ignore a result if there is nothing important in it

Returns:

undefined if thhis JrResult is blank

addToSession(req, flagAddToTop)

Source:

Add this JrResult to the (request)session, so it can be remembered for flash error messages (when we show an error on their next/redirected page request)

Notes
  • We add the mergeIn function to combine multiple jrResults into one
Parameters:
Name Type Description
req *

express request

flagAddToTop *

add it to the top of the list of such error messages

Returns:

this

clear()

Source:

clear all fields of the jrResult object

(private) clearSection(key)

Source:

Generic helper to clear all items in the given section (errors, success, messages)

Parameters:
Name Type Description
key *
Returns:

this

copyFrom(source)

Source:

Just a shallow copy of the object

Parameters:
Name Type Description
source object

JrResult

Returns:

this

getErrorsAsString()

Source:

Return a string containing all errors in the result

Returns:

error string

getExtraData(key, defaultVal)

Source:

Get generic extra data for the result

Parameters:
Name Type Description
key string

key to retrieve data for

defaultVal *

returned if key not set

Returns:

extra data stored under key, or defaultVal if none

getFieldError(key, defaultval)

Source:

Gets any error associated with this field key, or defaultVal if none set (defaults to undefined)

Parameters:
Name Type Description
key string
defaultval *

(if not provided, undefined will be used)

Returns:

error message associated with key, or defaultVal if none set

getSuccessAsString()

Source:

Return a string containing all non-errors in the result

Returns:

success string

getType()

Source:

Get the type value for the object which can be set

isError()

Source:

Checks if the jrResult is (contains) an error

Returns:

true if there are any error items pushed into this jrResult

mergeIn(source, flagMergeSourceToTop)

Source:

This function is used to merge one result into another, combining errors and successes, etc. It's a rather elaborate function but serves an important purpose when we have multiple results and we care about errors in either.

Parameters:
Name Type Description
source object

source JrResult

flagMergeSourceToTop boolean

whether to merge souce object messages above ours when combinging

Returns:

this

mergeInThenRemoveFromSession(flagSessionAtTop)

Source:

Get any existing result from session, merging it with any provided here, and clearing any existing result from session

Parameters:
Name Type Description
flagSessionAtTop *

if true, result errors will be placed at top

Returns:

the new combined result in the session

(private) push(sectionKey, msg, flagOnTop)

Source:

Generic helper to push a message into a given section (errors, success, messages)

Parameters:
Name Type Description
sectionKey string
msg *

message to add

flagOnTop *

if true, the new message will be pushed at top of list

Returns:

this

pushBiFieldError(key, shortMsg, longMsg)

Source:

Set the error associated with a specific key, AND adds a different error message as a general error

Parameters:
Name Type Description
key string

the field name to set the error for

shortMsg string

the error messsage to add to the field

longMsg string

the error message to add as a generic error

Returns:

this

pushError(msg)

Source:

Add a generic error to the result

Parameters:
Name Type Description
msg string
Returns:

this

pushErrorOnTop(msg)

Source:

Add a generic error to the result, pushing it to the top of the error list

Parameters:
Name Type Description
msg string
Returns:

this

pushException(msg)

Source:

Add a generic exception error to the result

Parameters:
Name Type Description
msg string
Returns:

this

pushFieldError(key, msg)

Source:

Set the error associated with a specific key

Notes:
  • this also causes a general (non-feild) error to be added to the object, with the same message
Parameters:
Name Type Description
key string

the field name to set the error for

msg string

the error messsage

Returns:

this

pushMessage(msg, flagOnTop)

Source:

Add a generic message (not an error) to the result

Parameters:
Name Type Description
msg string
flagOnTop boolean

if true the message will be pushed on top; if unspecified it will not

Returns:

this

pushSuccess(msg, flagOnTop)

Source:

Add a success message (not an error) to the result

Parameters:
Name Type Description
msg string
flagOnTop boolean

if true the message will be pushed on top; if unspecified it will not

Returns:

this

setError(msg)

Source:

Clear any previous error and then add a generic error to the result

Parameters:
Name Type Description
msg string
Returns:

this

setExtraData(key, val)

Source:

Set generic extra data for the result

Parameters:
Name Type Description
key string

key to store data in

val *

data to store

Returns:

this

setFieldError(key, value)

Source:

Set a field-associated error; marks the result as an error and makes it possible for observer to check which specific field caused it

Parameters:
Name Type Description
key string
value *
Returns:

this

setType(typestr)

Source:

Set the type value which can be checked later

Parameters:
Name Type Description
typestr *