The once feature is used in almost all core behaviors. The once feature is implemented as a jQuery plugin, making it impossible to remove jQuery from core scripts.
Implement a version of the once feature that does not depend on jQuery
https://www.drupal.org/project/once → is considered Drupal Core, there are no runtime dependencies. For dev dependencies see #3199444: Dependency evaluation →
None
core/once
libraryonce
global variable to eslint configonce
, once.filter
(equivalent to jQuery.fn.findOnce), once.remove
, once.find
(new function not previously possible with the jQuery implementation)data-once
attribute to each element that holds all the once ids calledThe once library has been added to replace the functionality provided by for jQuery .once function. The change record for the once library → details how to use the new functionality.
# mymodule.libraries.yml
myfeature:
js:
js/myfeature.js: {}
dependencies:
- core/drupal
- core/jquery
- core/jquery.once
# js/myfeature.js
(function ($, Drupal) {
Drupal.behaviors.myfeature = {
attach(context) {
const $elements = $(context).find('.myfeature').once('myfeature');
}
};
}(jQuery, Drupal));
# mymodule.libraries.yml
myfeature:
js:
js/myfeature.js: {}
dependencies:
- core/drupal
- core/once
# js/myfeature.js
(function (Drupal, once) {
Drupal.behaviors.myfeature = {
attach(context) {
const elements = once('myfeature', '.myfeature', context);
}
};
}(Drupal, once));
This adds the core/once library, a standalone library that offers the same benefits as core/jquery.once but without the jQuery dependency.
>Inspired from http://eleks.github.io/js2js/. Now I totally rewritten jQuery.once into Plain JavaScript Code. Aiming to provide a modern pattern widely used everywhere, not just the Drupal way.
Features / Changes:
Performance testing result:
https://docs.google.com/spreadsheets/d/1KXVKZS-HJhbQFgUYUmzPzMpJpEMOPXyG...
** Noticeable performance diff on iPhones.
Testing Repo:
https://github.com/KayLeung/dropletOnce
The reason I do not like jQuery.Once 2.0:
Fixed
9.2
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.