Skip to content

grace-plugins/grace-hotwire

Repository files navigation

Main branch build status Apache 2.0 license Latest version on Maven Central Grace on X

Grace with Hotwire

Grace is an open-source and powerful One-Person web application Framework to help developers quickly build Spring Boot applications written in the Groovy programing language.

Turbo gives you the speed of a single-page web application without having to write any JavaScript.

Stimulus is a JavaScript framework with modest ambitions. It doesn’t seek to take over your entire front-end—in fact, it’s not concerned with rendering HTML at all. Instead, it’s designed to augment your HTML with just enough behavior to make it shine.

This plugin will help you to develop Grace app with Hotwire Turo and Stimulus.

Usage

Add dependency to the build.gradle,

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.graceframework.plugins:stimulus:VERSION"
    implementation "org.graceframework.plugins:turbo:VERSION"
}

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"/>

Development

Build from source

git clone https://github.com/grace-plugins/grace-hotwire.git
cd grace-hotwire
./gradlew publishToMavenLocal

Support Version

  • Grace 2022.0.0+
  • Grails 3.0+

Roadmap

1.x

  • Stimulus 3.2.2
  • Turbo 8.0.4

License

This plugin is available as open source under the terms of the APACHE LICENSE, VERSION 2.0

Links