Methods
toHtml() → {string}
- Source:
Returns:
A somewhat pretty formatted HTML snippet.
- Type
- string
toSseMessage() → {string}
- Source:
Returns:
A single SSE "data" message with a single-line HTML snippet,
with a double-newline suffix.
- Type
- string
toWebSocketMessage() → {string}
- Source:
Returns:
A single-line HTML snippet.
- Type
- string
turboStream(mimeType) → {turboStream}
Turbo stream middleware factory function.
Parameters:
Name | Type | Description |
---|---|---|
mimeType |
string | The mime type to check for in the HTTP Accept header, to detect the presence of a request made by the Turbo client, as well as the type to send in the Content-Type header, in response to such requests. |
- Source:
Returns:
Turbo stream middleware.
- Type
- turboStream
Type Definitions
compileView(spec) → {String}
Function for compiling a single view template into a turbo-stream
HTML snippet.
Parameters:
Name | Type | Description |
---|---|---|
spec |
TurboStreamSpec | an object conforming to TurboStreamSpec type. |
- Source:
Returns:
- HTML containing a the rendered view, wrapped in
a tag,
- Type
- String
compileViews(specs) → {String}
Function for compiling multiple view templates into turbo-streams
Parameters:
Name | Type | Description |
---|---|---|
specs |
Array.<TurboStreamSpec> | an object conforming to TurboStreamSpec type. |
- Source:
Returns:
- HTML containing a set of tags,
each wrapping one of the views provided. Tags are separated by a
newline (\n).
- Type
- String
Example
<pre>
res.turboStream.compileViews(
[
{
stream: {
action: 'append',
target: 'item-list',
},
locals: { items, cursor: nextCursor },
view: 'item-list/partials/item-list',
},
{
stream: {
action: 'replace',
target: 'item-list-more-button',
},
locals: { cursor: nextCursor, hasMore },
view: 'item-list/partials/item-list-more-button',
},
],
true
);
</pre>
renderViews(specs)
Convenience function that calls compileViews() then sendStream()
with the results.
Parameters:
Name | Type | Description |
---|---|---|
specs |
Array.<TurboStreamSpec> | an object conforming to TurboStreamSpec type. |
- Source:
sendStream(res, html, onlyFormatopt, mimeTypeopt)
Convenience function that sends (already rendered) html with the
turbo-stream MIME type.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
res |
object | The express response object. | ||
html |
string | The rendered html. | ||
onlyFormat |
boolean |
<optional> |
false | If true, will wrap the response in a "res.format" object, so that the route will _only_ respond to the Turbo mime type, or return a HTTP 406 (Not Acceptable) response. |
mimeType |
string |
<optional> |
MIME_TYPE | The mime type to use for the Content-Type header. Defaults to value in the constants file. |
- Source:
turboAction(view, localsopt, stream, onlyFormatopt)
Function which will render a response with the a given
template and locals, wrapped in a tag.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
view |
string | Path to the view to render inside
the |
||
locals |
object |
<optional> |
locals to pass on to the view, in the same format as is done for req.render. | |
stream |
object | Attributes to set in the |
||
onlyFormat |
boolean |
<optional> |
false | If true, will wrap the response in a "res.format" object, so that the route will _only_ respond to the Turbo mime type, or return a HTTP 406 (Not Acceptable) response. |
- Source:
turboStream
middleware.
Middleware which decorates the express response object with a
"turboStream" object, with the following properties:
Type:
- object
Properties:
Name | Type | Description |
---|---|---|
append |
turboAction | Function to generate a turbo stream with the "append" action. |
prepend |
turboAction | Function to generate a turbo stream with the "prepend" action. |
replace |
turboAction | Function to generate a turbo stream with the "replace" action. |
update |
turboAction | Function to generate a turbo stream with the "update" action. |
compileView |
compiledView | See compileView jsdoc. |
compileViews |
compiledViews | See compileViews jsdoc. |
sendStream |
sendStream | See sendStream jsdoc. |
TurboStream |
class | See TurboStream jsdoc. |
- Source:
TurboStreamSpec
Object with necessary properties to render a view with optional locals
and wrap it in a tag.
Type:
- object
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
stream |
object | Attributes to set in the |
|
view |
string | Path to the view to render inside
the |
|
locals |
object |
<optional> |
locals to pass on to the view, in the same format as is done for req.render. |
- Source: