vaadin / vaadin/framework

Publish a "document ready" event for included javascript functions

Open
#3,075 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
1.8k
Forks
717
Avg merge
2d 6h
Merged PRs (30d)
3

Description

Originally by cpoile


I have multiple use cases requiring a callback from the framework to let my attached JavaScript functions know that they can now begin work on the DOM. Similar to a $(document).ready from jQuery.

According to a helpful support request response from Leif, I understand that attached Javascript is evaluated very early in the layout cycle. So if I include a JavaScript file in the @JavaScript annotation, or in a CustomLayout layout file, it is evaluated before the page is ready in the browser. This is causing me days of grief as I try to find workarounds.

Currently I am placing all Javascript into a document-level event handler like so:

$(document).on('mouseenter', 'body', function (e) {
   "use strict";
   if (tabSwitchInitialized === false) {
      tabSwitchInit();
      tabSwitchInitialized = true;
   }
});

This is a giant hack, as you can see. The jQuery callback $(document).ready() does not work as it should, or else I would use it. I believe this would be a very useful feature (for evidence, see how many JavaScript functions are wrapped in jQuery's $(document).ready()).

Instead, can we register a VAADIN.ready() function of some kind? It would help me most if this function was triggered every time the page is re-displayed, such as in the following instances:

  • on first load
  • on reload
  • on entering and leaving the page

Thank you kindly.


Imported from https://dev.vaadin.com/ issue #10063

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how JavaScript included through the @JavaScript annotation and CustomLayout layouts is evaluated, then inspect the browser lifecycle around initial load, reload, and page navigation. The requested outcome is a framework event that attached JavaScript can use when the DOM is ready on each relevant redisplay; the issue does not name files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.