From 017eeda3d453a9b6c1ea45157ac4c3453e5ebef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 4 Aug 2020 18:56:01 +0200 Subject: [PATCH] Remove redundant call to $(document).ready() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `$()` function is a shortcut for `$(document).ready()`, and we were attaching events to `$(document).ready()` inside the `$()` function. In a similar way, we were handling the `page:load` and `ajax:complete` events inside the `$()` function. But when those events trigger, the DOM is already ready. Besides, we don't have to wait for the DOM to be ready before attaching events to the `document` element. Quoting jQuery's `.on()` documentation: > The document element is available in the head of the document before > loading any other HTML, so it is safe to attach events there without > waiting for the document to be ready. Co-Authored-By: Senén Rodero Rodríguez --- app/assets/javascripts/application.js | 5 ++--- app/assets/javascripts/stat_graphs.js | 10 +++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index cfdf34113..d3c84a255 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -170,7 +170,6 @@ $(function() { "use strict"; Turbolinks.enableProgressBar(); - - $(document).ready(initialize_modules); - $(document).on("page:load", initialize_modules); }); +$(document).ready(initialize_modules); +$(document).on("page:load", initialize_modules); diff --git a/app/assets/javascripts/stat_graphs.js b/app/assets/javascripts/stat_graphs.js index d3aa121fa..97d743355 100644 --- a/app/assets/javascripts/stat_graphs.js +++ b/app/assets/javascripts/stat_graphs.js @@ -9,10 +9,6 @@ var initialize_stats_modules = function() { App.Stats.initialize(); }; -$(function() { - "use strict"; - - $(document).ready(initialize_stats_modules); - $(document).on("page:load", initialize_stats_modules); - $(document).on("ajax:complete", initialize_stats_modules); -}); +$(document).ready(initialize_stats_modules); +$(document).on("page:load", initialize_stats_modules); +$(document).on("ajax:complete", initialize_stats_modules);