The return of Rails UJS
Unobtrusive Javascript is an easy way to added single page app like features to HTML links and form tags. Its taken a backseat since the introduction of Hotwire, but Superglue puts UJS back in the forefront and packs it with functionality that makes building SPA-like functionality easy and consistent.
Want to reload a shopping cart?
Or maybe load a modal efficiently when the next page has one?
With Superglue, there is just one concept. No need for the complexity of Stimulus controllers, Turbo Streams, or Turbo Frames.
Navigating with UJS
Superglue operates like a multipage application. In other to transition to the next page without reloading you'll need to use UJS attributes data-sg-remote
or data-sg-visit
.
data-sg-visit
data-sg-visit
Use data-sg-visit
when you want to navigate to the next page and update the address bar without reloading.
In the above example, when the link is clicked, Superglue will intercept the click, make a request for /posts/new.json
, swap your page component, and pass the payload.
!!! note You are not able to specify the HTTP method used in a UJS link.
You can also use data-sg-visit
on forms:
data-sg-remote
data-sg-remote
Use data-sg-remote
when you want to update parts of the current page without reloading the screen.
!!! tip "Differences from remote
" The only difference between data-sg-remote
and remote
, is that data-sg-remote
passes the current page as the target pageKey
of remote
.
Combine this with props_template's [digging] to selectively load content.
You can also use data-sg-remote
on forms.
Expanding UJS
data-sg-replace
data-sg-replace
Last updated
Was this helpful?