简体   繁体   中英

Whats the best way to manage JavaScript with Ruby on Rails 6?

I'm giving a try to RoR 6 (I'm coming from MEAN and I've not touched RoR from version 3) and I'm finding some troubles to find the best way to manage JavaScript code. Maybe because of my background.

I've read a lot about the topic (including official guides https://guides.rubyonrails.org/working_with_javascript_in_rails.html ) but It seems official documentation is out of date.

According to documentation, when you generate a controller from cli it should create a.js file for that controller but it doesn't occur. Besides, right now Webpack has been added to RoR 6 and JavaScript is no longer managed by Asset Pipeline (Am I right?) but there's not any reference to this matter.

I want to find a way to write code JS for every view and keep that code isolated from the rest.

  • Where should I put all the JS code?
  • How can I get isolation for the JS code of every view?
  • I've added jQuery to the project due to Bootstrap (by using Yarn) and to Webpack this way but $ or jQuery is undefined:
const { environment } = require('@rails/webpacker')

const webpack = require("webpack")

environment.plugins.append("Provide", new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
}))

module.exports = environment

I'd appreciate some help.

Thanks!

SOLUTION:

I found what I was looking for --> https://stimulusjs.org

Stimulusjs, created by Basecamp, adds a JS layer to every HTML view and let us to keep order and clarity when writing JS code. It connects the JS file with the DOM and nothing more. Enough to add some JS to improve functionality.

It pairs perfectly with Turbolinks and is ready to be used with Webpack. Besides, it can be learned in 10 minutes (no more). Installation is also absurdly easy.

Anyway, if you need to get some knowledge about RoR and Webpack/Webpacker, you can visit these links:

And finally, if you don't wanna use a JS framework like Stimulus for managing JS code under RoR, you can always try these gems for specific page JS:

The change to Webpack is very new and the documentation has not quite caught up.

Generating asset files when running the generator was only done with the old assets pipeline and even then was a not really good idea. It relied on Sprockets special require_tree directive that would slurp up all the files in the directory and add them to the manifest. In alphabetical order, so you had no control over the order of execution.

It also fooled beginners into thinking that the js they put into users.js was only executed in their users controller when in fact it was all just slurped up into a single manifest.

With Webpack you explicitly import assets.

Where should I put all the JS code?

You're encouraged to place your actual application logic in a relevant structure within app/javascript .

How can I get isolation for the JS code of every view?

While you can use javascript_pack_tag in the view itself to require specific files this is not really a good idiom as it creates unnecessary HTTP requests and logic that is hard to follow.

If you want to ensure that code is executed when a particular view loads you can add data attributes to the body tag and create special events:

# app/layouts/application.html.erb
<body data-action="<%= action_name >" data-controller="<%= controller_name %>">

// fired when turbolinks changes pages.
$(document).on('turbolinks:load', ()=>{
  let data = $(body).data();
  // replace myns with whatever you want
  $(this).trigger(`myns:${data.controller}`, data)
         .trigger(`myns:${data.controller}#${data.action}`, data)
         .trigger(`myns:#${data.action}`, data)
});

Then you can wrap functionality that should happen when a special page loads by listening for your custom events.

$(document).on('myns:users#show', ()=>{
  console.log("We are on users#show");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM