require_tree
directive (and we’ll talk about them) are compressed into one file and, accordingly, work on all pages. But what to do if a piece of javascript is needed only on one page and is not needed at all on another? <% content_for :head do %> <%= javascript_include_tag 'my_fancy_js' %>
require_tree
. But the disadvantage of this approach is that the scripts belonging to a particular controller are not in the same place (which means it is harder to search and understand). In the appendage, if a piece of code is large, then we lose the advantages of the pipeline. The solution used in the jam minimizes the disadvantages described. rails generate pluggable_js Post index new
Post.index()
and Post.new()
functions will be called after the DOM loads: app/assets/javascripts/pluggable/posts/index.js.coffee app/assets/javascripts/pluggable/posts/new.js.coffee
window.Post ||= {} Post.index = () -> # your code goes here Post.new = () -> # and here
<%= javascript_pluggable_tag %>
specified in the layout will connect the file if it matches the path and values of the current parameters of the controller / action. Yes, there is an additional request to the server, but this is just a trigger.Post.index()
function Post.index()
also be called. To do this, you can create config / initializers / pluggable_js.rb and use the setting: PluggableJs.config do |config| config.pair_actions = { 'search' => 'index' } end
{ 'create' => 'new', 'update' => 'edit' }
set by default.Source: https://habr.com/ru/post/194072/
All Articles