ViewEngineUnit

libflitter/views/ViewEngineUnit~ ViewEngineUnit

The view engine unit is responsible for registering the view engine with the underlying Express installation. It also defines a function for instantly creating route handlers from view names. This unit sets Pug as the view engine, though that would be relatively easy to alter.


Constructor

new ViewEngineUnit(views_diropt)

Instantiate the unit class. Resolves the fully-qualified path to the views directory.

Parameters:
Name Type Attributes Default Description
views_dir string <optional>
'./app/views'

path to the views directory

Extends

Members

(static) name

Get the name of the service provided by this unit: 'views'

(static) services

Defines the services required by this unit.

(private) _di_allow_defer :boolean

If true, the injector will defer the class if the class requests any services that the container is missing. These services are filled in later and added to the prototype and any instances. True by default.

Type:
  • boolean

(private) _di_deferred_instances :Array.<module:flitter-di/src/Injectable~Injectable>

Collection of instances of this class that need to have the deferred service instances injected into them when the deferred services are finally provided.

(private) _di_deferred_services :Array.<string>

List of services that were deferred and not provided at the time of injection.

Type:
  • Array.<string>

directory :string

The fully-qualified path to the views directory.

Type:
  • string

Methods

(async) cleanup(app) → {Promise.<void>}

Cleans up the unit's resources before Flitter closes.

Parameters:
Name Type Description
app module:libflitter/app/FlitterApp~FlitterApp

the Flitter app

Returns:
Type
Promise.<void>

directories() → {Object}

Get the directories provided by this unit. Currently, "views" mapped to module:libflitter/views/ViewEngineUnit~ViewEngineUnit#directory.

Returns:
Type
Object

(async) error(response, status, params) → {Promise.<*>}

Sends an HTTP error and renders the error view with the corresponding status code. If the status code cannot be resolved to an integer, the request status will default to 400.

Parameters:
Name Type Description
response Express/Response

the Express response to be served

status string | int

HTTP or error status code

params Object

collection of arguments to be passed to the view. The "code" key will be overwritten with the status.

Returns:
Type
Promise.<*>

(async) go(app, context) → {Promise.<void>}

Loads the unit. Binds the view helper function to the global context and configures Express to use the Pug view engine.

Parameters:
Name Type Description
app module:libflitter/app/FlitterApp~FlitterApp

the Flitter app

context module:libflitter/Context~Context

the unit's context

Returns:
Type
Promise.<void>

migrations() → {string}

Get the fully-qualified path to the migrations provided by this unit.

Returns:
Type
string

name() → {String}

Get the name of the unit. Should be a lowercase, alphanum/dash/underscore string.

Returns:
Type
String

status(setopt) → {string}

Get or set the unit's current status.

Parameters:
Name Type Attributes Description
set string <optional>

if provided, set the status of the unit

Returns:
  • current status of the unit
Type
string

(async) view(response, view_name, args, resource_list) → {Promise.<*>}

Render a view to the provided response. Passes through arguments and retrieves the specified resources for the view.

Parameters:
Name Type Description
response Express/Response

the Express response to be served

view_name string

the Flitter canonical name of the view to be served

args Object

collection of arguments to be passed directly to the view

resource_list Array.<module:libflitter/views/ViewEngineUnit~ViewEngineUnit~ResourceSpec>

array of resource specifications to be retrieved and passed to the view

Returns:
Type
Promise.<*>