repositories {
mavenCentral()
}
dependencies {
implementation "org.graceframework.plugins:stimulus:VERSION"
implementation "org.graceframework.plugins:turbo:VERSION"
}
2 Usage
Version: 0.5.0-SNAPSHOT
2 Usage
Add dependency to the build.gradle
,
Turbo plugin supports controller-specific withFormat()
method,
class BookController {
def list() {
def books = Book.list()
withFormat {
turbo_stream {
render(template: "book", model: [bookList: books])
}
json {
render books as JSON
}
}
}
}
Also, this plugin supports extendsions for Grails Request and Response,
// You can get Turbo request headers from Grails Request
request.turboFrameId == request.getHeader('Turbo-Frame')
request.turboRequestId == request.getHeader('X-Turbo-Request-ID')
// Check if Turbo Request?
if (request.isTurboRequest()) {
template = 'book-detail'
}
// Check if Turbo Frame?
if (request.isTurboFrame()) {
template = 'book-detail'
}
// Check if Turbo Stream?
if (request.isTurboStream()) {
template = 'book-detail'
}
If you use respond
method introduced in Grails 2.3. The respond method tries to produce the most appropriate response for the requested content type (JSON, XML, HTML etc.)
This plugin already provides Mime Types for Turbo Stream.
For example given the show action:
def show(Book book) {
respond book
}
You could supply a show.turbo_stream.gsp
file to render the Turbo Stream:
<turbo-stream action="append" target="books">
<template>
...
</template>
</turbo-stream>
If you use asset-pipeline
plugin, this plugin already includes stimulus.js
, turbo.js
,
so you can add stimulus.js
to the app/assets/application.js
,
//= require stimulus
//= require turbo
//= require_self
Also, you can use asset
tag in the GSP,
<asset:javascript src="stimulus.js"/>
<asset:javascript src="turbo.js"/>