Options
All
  • Public
  • Public/Protected
  • All
Menu

Class View

View represents a view object in the MVC pattern.

View provides a set of methods (e.g. render) for rendering purpose.

View is configured as an application component in Application by default. You can access that instance via Application.view.

For more details and usage information on View, see the guide article on views.

author

Mahesh S Warrier https://github.com/codespede

Hierarchy

Index

Constructors

  • new View(config: {}): View

Properties

basePath: string = 'static'

The base path for JS and CSS files. Defaults to the folder 'static' in the application's root.

cssFiles: string[] = []

The registered CSS files.

see

registerFile

initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

js: { begin: string[]; end: string[]; head: string[]; load: string[]; ready: string[] } = ...

The registered JS code blocks

see

registerJs

Type declaration

  • begin: string[]
  • end: string[]
  • head: string[]
  • load: string[]
  • ready: string[]
jsFiles: { end: string[]; head: string[] } = ...

The registered JS files.

see

registerFile

Type declaration

  • end: string[]
  • head: string[]
layout: string | false = '/layouts/main.ejs'

The name of the layout to be applied to the views. This property mainly affects the behavior of render. If false, no layout will be applied.

Methods

  • endPage(ajaxMode?: boolean): string
  • Marks the ending of an HTML page.

    Parameters

    • ajaxMode: boolean = false

      whether the view is rendering in AJAX mode. If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts.

    Returns string

  • findViewFile(view: string): any
  • Finds the view file based on the given view name.

    Parameters

    • view: string

      the view name or the path alias of the view file. Please refer to [[render()]] on how to specify this parameter.

    Returns any

    the view file path. Note that the file may not exist.

  • head(): string
  • init(): Promise<void>
  • publishAndRegisterFile(file: string, position?: string): Promise<void>
  • Publishes and registers the given file at given position

    Parameters

    • file: string

      the relative file path

    • position: string = 'end'

      position at which file should be placed. Possible values are head, begin, ready, load, end

    Returns Promise<void>

  • registerFile(type: "js" | "css", url: string, options?: {}, key?: any): void
  • Registers a JS or CSS file.

    Parameters

    • type: "js" | "css"

      type (js or css) of the file.

    • url: string

      the JS file to be registered.

    • options: {} = {}

      the HTML attributes for the script tag. The following options are specially handled and are not treated as HTML attributes:

      • depends: array, specifies the names of the asset bundles that this CSS file depends on.
      • appendTimestamp: bool whether to append a timestamp to the URL.
      • [key: string]: any
    • key: any = null

      the key that identifies the JS script file. If null, it will use $url as the key. If two JS files are registered with the same key at the same position, the latter will overwrite the former. Note that position option takes precedence, thus files registered with the same key, but different position option will not override each other.

    Returns void

  • registerJs(js: string, position?: string): Promise<void>
  • Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.

    Parameters

    • js: string

      the JS code block to be registered

    • position: string = 'ready'

      the position in a page at which the JavaScript variable should be inserted. The possible values are:

      • head: in the head section. This is the default value.
      • begin: at the beginning of the body section.
      • end: at the end of the body section.
      • load: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
      • ready: enclosed within jQuery(document).ready(). Note that by using this position, the method will automatically register the jQuery js file.

    Returns Promise<void>

  • render(view: string, params?: {}, withLayout?: boolean): Promise<any>
  • Renders a view.

    The view to be rendered can be specified in one of the following formats:

    • absolute path within application (e.g. "/site/index"): the view name starts with a single slashes. The actual view file will be looked for under the view path of the application.
    see

    renderFile()

    Parameters

    • view: string

      the view name.

    • params: {} = {}

      the parameters (name-value pairs) that will be extracted and made available in the view file.

      • [key: string]: any
    • withLayout: boolean = true

      whether the view should be rendered inside a layout if it's available.

    Returns Promise<any>

    string the rendering result

  • renderEndHtml(ajaxMode: boolean): string
  • Renders the content to be inserted at the end of the body section. The content is rendered using the registered JS code blocks and files.

    Parameters

    • ajaxMode: boolean

      whether the view is rendering in AJAX mode. If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts.

    Returns string

    string the rendered content

  • renderFile(viewFile: string, params?: any): Promise<any>
  • Renders a view file.

    Parameters

    • viewFile: string

      the view file. This can be either an absolute file path or an alias of it.

    • params: any = {}

      the parameters (name-value pairs) that will be extracted and made available in the view file.

    Returns Promise<any>

    string the rendering result

  • renderHeadHtml(): string
  • Renders the content to be inserted in the head section. The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.

    Returns string

    the rendered content

  • setLayout(layout: string): View
  • Sets the layout for the this view instance.

    Parameters

    • layout: string

      the layout path

    Returns View

    the view instance itself

Generated using TypeDoc