<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joseph Kristofzski</title>
    <description>The latest articles on Forem by Joseph Kristofzski (@joe_66).</description>
    <link>https://forem.com/joe_66</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1068937%2F9afcf04c-87ad-42e5-8ce0-eddaab7ebbb7.jpg</url>
      <title>Forem: Joseph Kristofzski</title>
      <link>https://forem.com/joe_66</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/joe_66"/>
    <language>en</language>
    <item>
      <title>JavaScript Event Listeners, with Turbolinks and AJAX</title>
      <dc:creator>Joseph Kristofzski</dc:creator>
      <pubDate>Sun, 30 Apr 2023 21:40:30 +0000</pubDate>
      <link>https://forem.com/joe_66/javascript-event-listeners-with-turbolinks-and-ajax-j97</link>
      <guid>https://forem.com/joe_66/javascript-event-listeners-with-turbolinks-and-ajax-j97</guid>
      <description>&lt;p&gt;What best practices do you follow to maintain a clean, logical set of initializers in a Rails app, taking Turbolinks and AJAX into account?&lt;/p&gt;

&lt;p&gt;I've seen whole app initializes and single function listeners while looking through various open source apps. Some big Ruby on Rails app are set up like this:&lt;br&gt;
&lt;a href="https://github.com/forem/forem/blob/main/app/assets/javascripts/base.js.erb"&gt;https://github.com/forem/forem/blob/main/app/assets/javascripts/base.js.erb&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/gitlabhq/gitlabhq/blob/master/app/assets/javascripts/main.js"&gt;https://github.com/gitlabhq/gitlabhq/blob/master/app/assets/javascripts/main.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've been primarily working on smaller Rails apps, and I've found it easiest to set up standalone listeners, similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Turbolinks and AJAX listeners
$(document).on('turbolinks:render ajaxSuccess', function () {
  some_function();
}); 

// Document ready
// (Older versions of jQuery let me put ready in the first listener)
$(document).ready(function() {
  some_function();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click and change listeners are added similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$(document).on('click', '.my-class', function() {
  some_function();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>rails</category>
      <category>jquery</category>
    </item>
  </channel>
</rss>
