
Security News
Researcher Exposes Zero-Day Clickjacking Vulnerabilities in Major Password Managers
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Pastur provides a JavaScript pattern that lets you easily write and organize page-specific code that works properly with Turbolinks. Or, page-specific JS for turbolinks!
Inspired by Brandon Hilkert.
When navigating between pages in Rails, Turbolinks only reloads the parts of the page that change. In the typical setup, JavaScript is often only loaded when a visitor first accesses the web app, and not on every page load. This is great for page load speed while navigating around an app, but it can cause problems for things like event binding, widget initialization, etc.
Pastur provides a clean pattern to define all of the app's event bindings on a full load, then only fire them on the proper pages. It's a really simple abstraction - seriously, the readme is longer than the code.
Pastur is intended to enhance an out-of-the-box Rails setup with a Sprockets asset pipeline, but there's nothing in the code base that depends on them.
You've seen this before:
# Gemfile
gem 'pastur'
$ bundle install
Or:
$ gem install pastur
Add to your JavaScript manifest, after jQuery and Turbolinks:
//= require pastur
//= require_tree ./pages
You can organize and include the require_tree
however you want, as long as Sprockets loads it after pastur
.
// app/assets/javascripts/pages/whatever.js
App.page('.controller.action', [
App.event('turbolinks:load', function(e) {
// page-specific code goes here
}),
App.event('click', '#selector', function(e) {
// page-specific code goes here
}),
// ...
]);
App.page()
takes two arguments:
App.event()
s, mirroring the syntax of $(document).on()
Basically all this does is inject a check for the presence of $('.controller.action')
on the page before running event code. The recommended convention is to set <body class="controller action">
and define the page selector as .controller.action
, but this is very flexible. Page selectors don't even have to be page-specific and should also work with partials.
Event bindings are only defined once, and all page-specific JS should only be run in an event context. Reusable components should be defined elsewhere, outside of Pastur (but called from within Pastur).
Pastur also works with CoffeeScript:
App.page '.controller.action', [
App.event 'turbolinks:load', (e) ->
# awesome
]
FAQs
Unknown package
We found that pastur demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.