From 37c6f42823258816c4f7afe804773589b53641b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20=C4=90uza?= Date: Sat, 24 Oct 2020 10:52:31 +0200 Subject: [PATCH] Set up control events after document loaded --- app/views/render_async/_request_jquery.js.erb | 14 +++++++++----- app/views/render_async/_request_vanilla.js.erb | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/views/render_async/_request_jquery.js.erb b/app/views/render_async/_request_jquery.js.erb index 0c1d04a..2edb788 100644 --- a/app/views/render_async/_request_jquery.js.erb +++ b/app/views/render_async/_request_jquery.js.erb @@ -122,13 +122,17 @@ if (window.jQuery) { } } - var container = $("#<%= container_id %>"); + function _setUpControlEvents() { + var container = $("#<%= container_id %>"); - // Register a stop polling event on the container - $(container).on('async-stop', _clearRenderAsyncInterval) + // Register a stop polling event on the container + $(container).on('async-stop', _clearRenderAsyncInterval) - // Register a start polling event on the container - $(container).on('async-start', _renderAsyncFunction) + // Register a start polling event on the container + $(container).on('async-start', _renderAsyncFunction) + } + + _runAfterDocumentLoaded(_setUpControlEvents) <% if turbolinks %> $(document).one('turbolinks:visit', _clearRenderAsyncInterval); diff --git a/app/views/render_async/_request_vanilla.js.erb b/app/views/render_async/_request_vanilla.js.erb index 4484351..be3700a 100644 --- a/app/views/render_async/_request_vanilla.js.erb +++ b/app/views/render_async/_request_vanilla.js.erb @@ -132,13 +132,17 @@ } } - var container = document.getElementById('<%= container_id %>'); + function _setUpControlEvents() { + var container = document.getElementById('<%= container_id %>'); - // Register a polling stop event on the container - container.addEventListener("async-stop", _clearRenderAsyncInterval) + // Register a polling stop event on the container + container.addEventListener("async-stop", _clearRenderAsyncInterval) - // Register a start polling event on the container - container.addEventListener("async-start", _renderAsyncFunction) + // Register a start polling event on the container + container.addEventListener("async-start", _renderAsyncFunction) + } + + _runAfterDocumentLoaded(_setUpControlEvents) <% if turbolinks %> document.addEventListener("turbolinks:visit", _clearRenderAsyncInterval)