Skip to content

Commit 0f5cf8a

Browse files
knowlerQWp6t
authored andcommitted
[Sage 10] Replace jQuery’s .ready() (#2182)
* Use DOMContentLoaded listener for document ready * Account for DOMContentLoaded not firing * Extract ready() to utils.js Also uses `window.setTimeout(fn, 0)` to cover async. In a commit/PR in the near future, the `util` directory will be going away since the DOM router has been extracted to its own package (https://github.com/roots/js-dom-router). `utils.js` will function as a place for custom utilities, similar to what `app/helpers.php` is on the PHP side of things.
1 parent 04102fa commit 0f5cf8a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

resources/assets/scripts/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'bootstrap';
1010
import Router from './util/Router';
1111
import common from './routes/common';
1212
import aboutUs from './routes/about';
13+
import { ready } from './utils';
1314

1415
/**
1516
* Populate the Router instance with DOM routes.
@@ -25,4 +26,4 @@ const routes = new Router({
2526
/**
2627
* Load Events
2728
*/
28-
jQuery(document).ready(() => routes.loadEvents());
29+
ready(() => routes.loadEvents());

resources/assets/scripts/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Execute a function when the DOM is fully loaded.
3+
*
4+
* @param {function} fn
5+
*/
6+
export const ready = fn => document.readyState !== 'loading'
7+
? window.setTimeout(fn, 0)
8+
: document.addEventListener('DOMContentLoaded', fn);

0 commit comments

Comments
 (0)