Use JS events instead of Drupal.behaviors

Created on 19 February 2012, over 12 years ago
Updated 30 July 2024, 3 months ago

I'm proposing using javascript events instead of Drupal.behaviors.

Problems

  • It's a big Drupalism
  • Drupal.behaviors makes monkey-patching a recommended way to change an init function.
  • It's not flexible enough, contrib will build things that way and it ends up a terrible mess (see openlayers init function for an example). Using events would have helped dev to do the right thing.

Details

  • The context variable already exists in event.target. Nothing special to be done here.
  • It's possible to leverage custom events or to namespace events to allow scripts to bind a listener to the specific event it needs to react to.
  • We can pass additional data to the event callback, to add the settings and trigger values we have right now with behaviors.

Benefits

  • Remove a drupalism and help JS dev get into Drupal JS.
  • Much more flexible.
  • Loosely coupling is a better way to deal with this.

Also if we're considering the AMD proposal πŸ“Œ Use AMD for JS architecture Closed: won't fix , there won't be a need to "weight" the behaviors or to play with the weight option of drupal_add_js to have them run at the proper time. AMD takes care of dependencies and making sure are executed in the right order.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
JavascriptΒ  β†’

Last updated 5 days ago

Created by

πŸ‡«πŸ‡·France nod_ Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡«πŸ‡·France nod_ Lille

    Still relevant today. "DOM is slow and has a lot of overhead." this is not true anymore, the DOM is fine.

  • πŸ‡«πŸ‡·France pdureau Paris

    In πŸ“Œ Use webcomponents for dropbutton Needs review , @nod_ said "with webcomponents we don't need behaviors or once".

    And @brianperry shared about "html web components", components that add behaviors to mostly existing markup and don't use the shadow dom.

    So, i droppped this comment:

    Such an interesting issue! Replacing all Drupal behaviours by WebComponents looks exciting: one less Drupalism & better performance.

    Is it possible to do the same with "normal" markup instead of a custom element? Leveraging the is attribute and using the web component only as an (efficient) behaviour bag:

    <div is="drupal-dropbutton" class="dropbutton-wrapper dropbutton-multiple">
    <div class="dropbutton-widget">
    <!-- ul, li, li, li, li -->
    </div>
    </div>

    Advantages:

    • faster to convert the renderable once the JS is updated, just replace data- attributes by the is attribute
    • even faster rendering because server-side goodness
    • no accessibility issues because we are back to good old markup
    • Selenium may prefer this one :)

    Unfortunately, Safari would need a polyfill for now.

  • πŸ‡ΊπŸ‡ΈUnited States cosmicdreams Minneapolis/St. Paul

    @pdureau in your example above, what would the javascript look like. How would it use the "is" attribute?

  • πŸ‡«πŸ‡·France pdureau Paris

    For this example:

    <div is="drupal-dropbutton">...</div>
    

    The JS will look like that:

    class Dropbutton extends HTMLDivElement {
      // Logic & events
    }
    customElements.define('drupal-dropbutton', Dropbutton) ;
    

    I have not tested this yet, but the idea is to keep the server side rendering of the usual HTML markup, and use the web component class has a behaviour container, with a lifecycle managed by the browser instead of custom scripts.

  • πŸ‡ΊπŸ‡ΈUnited States cosmicdreams Minneapolis/St. Paul

    Ah, custom elements. If you're using custom elements, have you considered using Custom Events?

    See: https://www.youtube.com/watch?v=wmGmKSLfVCY

    This approach attempts to bubble up events from within a custom element, while also ensuring you have control over preventing events within the element from bubbling up.

Production build 0.71.5 2024