Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] reimplement $.ready() #19142

Conversation

rreckonerr
Copy link
Contributor

@rreckonerr rreckonerr commented Sep 19, 2020

TODOs

  • refactor waitForDOMReady away from jQuery
  • figure out if !this.$ === !hasDOM
  • fix failing tests in packages/@ember/application/tests/readiness_test.js
    • find a way to restore mocked readyState or a better way to mock it
    • improve coverage for advanceReadiness/deferReadiness cases

NOTES

  • jquery .ready() implementation:
    function completed() {
      document.removeEventListener( "DOMContentLoaded", completed );
      window.removeEventListener( "load", completed );
      jQuery.ready();
    }
    
    
    // Catch cases where $(document).ready() is called
    // after the browser event has already occurred.
    if ( document.readyState !== "loading" ) {
    
    
      // Handle it asynchronously to allow scripts the opportunity to delay ready
      window.setTimeout( jQuery.ready );
    
    
    } else {
    
    
      // Use the handy event callback
      document.addEventListener( "DOMContentLoaded", completed );
    
    
      // A fallback to window.onload, that will always work
      window.addEventListener( "load", completed );
    }

@rreckonerr
Copy link
Contributor Author

rreckonerr commented Sep 19, 2020

Fixes #19134

@rreckonerr rreckonerr changed the title [BUGFIX beta] reimplement $.ready() [WIP][BUGFIX beta] reimplement $.ready() Sep 19, 2020
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch from faaa31a to a85242c Compare September 19, 2020 11:18
@chancancode chancancode linked an issue Sep 20, 2020 that may be closed by this pull request
@chancancode
Copy link
Member

chancancode commented Sep 20, 2020

Thanks for looking into this. I talked to @rwjblue about this and he wanted to do some benchmarks to confirm this restoring the old behavior won't cause any regressions. I think he was planning to benchmark it against my workaround, but if this works then they can probably test this directly.

I also realized a problem with the approach here. I think we are not supposed to rely on document existing due to fastboot, but that presents a chicken-and-egg problem because I'm not sure how we would get the non-global document object to use here, since it's usually passed/injected via the boot() method as one of the options.

Maybe it's not a big deal since fastboot disables autoboot and wouldn't hit this code path anyway. @rwjblue/@krisselden do you have any thoughts? Is this just fine or should we at least guard accessing the document global and the event handling methods? (See comment thread below.)

@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch 3 times, most recently from 93fbb5d to 00f0941 Compare September 20, 2020 07:36
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch from 00f0941 to 18ad68e Compare September 20, 2020 08:02
@rreckonerr rreckonerr marked this pull request as ready for review September 20, 2020 08:18
@rreckonerr rreckonerr changed the title [WIP][BUGFIX beta] reimplement $.ready() [BUGFIX beta] reimplement $.ready() Sep 20, 2020
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch 3 times, most recently from 711aa84 to 0da70fc Compare September 21, 2020 08:05
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch from 0da70fc to b1537bb Compare September 28, 2020 07:56
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch 2 times, most recently from d1d7181 to ee264b5 Compare October 9, 2020 16:44
Since `jQuery.isReady = false;` is set before each test case.
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch from ee264b5 to 186ec12 Compare October 9, 2020 16:49
@rreckonerr rreckonerr force-pushed the bugfix/app-booted-before-dom-ready-without-jquery branch from 186ec12 to 88c59a4 Compare October 9, 2020 17:00
@rreckonerr
Copy link
Contributor Author

rreckonerr commented Oct 9, 2020

@chancancode
By the way, is $.ready() the last thing that stops us from passing foreign document object for iframe support?
It's mentioned in supported scenarios section in documentation:

For example, booting the instance in the full browser environment
while specifying a foreign document object (e.g. { isBrowser: true, document: iframe.contentDocument }) does not work correctly today,
largely due to Ember's jQuery dependency.

@rwjblue rwjblue merged commit b6c8a37 into emberjs:master Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

App booted before DOM ready without jQuery
3 participants