310 Unpoly [3d]

Updated . Posted . Visible to the public.

Unpoly Show archive.org snapshot is a library we use to get many advantages of SPAs Show archive.org snapshot , but with much less code.

Important

Work on this lesson in advisor mode.

Learning goals

  • You can explain what problem Unpoly solves: SPA-like navigation and interactions on a server-rendered app, with very little JavaScript.
  • You can explain how a fragment update works — a link or form targets a fragment, the server renders a normal page, only the target is swapped — and what the unpoly-rails gem adds on the server side.
  • You can enhance links and forms so they update fragments, submit automatically or navigate without full page loads, and configure this app-wide instead of per link.
  • You can build a subinteraction in an overlay: open a form in a new layer, show validation errors in place, and close the overlay and update the parent page on success.
  • You can attach JavaScript behavior to elements with compilers and clean up when elements are removed, and explain why that matters when pages are swapped instead of reloaded.
  • You replace hand-written JavaScript with Unpoly's built-in features wherever they fit.
  • You can explain how Unpoly compares to Hotwire/Turbo, the Rails default.

Resources

Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.

Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.

Exercises

Reduce JavaScript with Unpoly

Use Unpoly in MovieDB Show archive.org snapshot :

Unpoly overlays

  • The form to create a new actor should open in an overlay Show archive.org snapshot .
  • If the user submits an invalid form, validation errors should be shown within the same overlay.
  • When the user successfully saves a valid actor, the overlay should close and the list of actors should reload.

Note

Your movie DB's content security policy might block your inline callback function. You can wrap it with safe_callback Show archive.org snapshot to give it a valid nonce. 'up-on-accepted': up.safe_callback('up.validate( ...

Alternatives

Rails ships with Hotwire (Turbo and Stimulus) for the same job: updating fragments of a server-rendered page without full reloads. Turbo Drive and Turbo Frames correspond roughly to Unpoly's navigation and fragment targeting, Stimulus to compilers. Unpoly goes further with layers/overlays, form validation and its handling of complex form interactions, which is why it is our default — see Our Rails stack. You will meet Hotwire in tutorials and in some client projects; the concepts transfer.

Profile picture of Henning Koch
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2018-12-12 15:29)