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
The dataset of the element enabled with data-sg-visit
or data-sg-remote
is passed to your application_visit.js. You can add your own options to control the behavior of the UJS helpers. For example, if you want to selectively show a progress bar on some links.
data-sg-replace
data-sg-replace
Included in application_visit.js as an expanded option is data-sg-replace
. It pairs with data-sg-visit
to replace history instead of pushing when a user clicks on a form or a link. This can be useful when working with data tables with a large number of click-to-filter options that push history.
Last updated
Was this helpful?