PmacsRouteInfo
PMACS Frontend provides a meta-tag in your app's header which make available a JS object, PmacsRouteInfo
.
You can call this object to access three pieces of Rails-y information from inside your JS files:
PmacsRouteInfo.relative_url_root
PmacsRouteInfo.controller
PmacsRouteInfo.action
A possible use case for this information would be a guard clause at the top of a page-specific JS file:
if (PmacsRouteInfo.controller !== 'decisions' || PmacsRouteInfo.action !== 'index') return;
With Turbolinks or Turbo
This variable is calculcated in a head
script tag and is based on meta
tags in the DOM's head, so it will be correct and available to all scripts that wait for DOMContentLoaded/turbolinks:load
/turbo:load
events.
If a page navigation occurs with Turbolinks or Turbo, it is recalculated during the before-render
event and will remain correct and available to any scripts that wait for the DOM to load.