Last active
August 30, 2023 12:51
-
-
Save LittleCoding/d143cbb4e6076211e44d3bb27e706538 to your computer and use it in GitHub Desktop.
Basic jQuery independent ES6 JS for Drupal 9.2+ project (module, theme)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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