Skip to content

Instantly share code, notes, and snippets.

@LittleCoding
Last active August 30, 2023 12:51
Show Gist options
  • Save LittleCoding/d143cbb4e6076211e44d3bb27e706538 to your computer and use it in GitHub Desktop.
Save LittleCoding/d143cbb4e6076211e44d3bb27e706538 to your computer and use it in GitHub Desktop.
Basic jQuery independent ES6 JS for Drupal 9.2+ project (module, theme)
/**
* @file
* Behaviors for My Project.
*/
((Drupal, settings, once) => {
/**
* Holds functions, methods, settings and other information for My Project.
*
* @namespace
*/
Drupal.myproject = {
preInit(context) {},
init(settings) {},
postInit() {},
};
/**
* Attaches the My Project behaviors for STUFFS.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Call pre-init function when the DOM is ready.
* Call init function when DOM content is loaded but before images load.
* Call post-init function when the page has loaded.
*/
Drupal.behaviors.myproject = {
attach(context, settings) {
Drupal.myproject.preInit(context);
window.addEventListener('DOMContentLoaded', () => {
once('myprojectInit', 'html', context).forEach(() => {
Drupal.myproject.init(settings);
window.addEventListener('load', () => {
Drupal.myproject.postInit();
});
});
});
},
};
})(Drupal, drupalSettings, once);
global-scripts:
js:
js/myproject.js: {}
dependencies:
- core/drupal
- core/drupalSettings
- core/once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment