0

jQuery suggests to wait for the document ready event before doing any DOM manipulation. In flight.js components, this.after('initialize') seems to be called before that event triggers.

Is it necessary to wait for it within a flight component, like so?

this.after('initialize', function () {

    // Is this necessary?

    $(document).ready(function () {
        ...
    });

    // Or this, the flight way?

    this.on(document, 'ready', function () {
        ...
    });

});

Thank you

Rudi
  • 2,987
  • 1
  • 12
  • 18
  • There is far too little context here to understand what this code is trying to do. For example, what is `this` in your code snippet? What is the `.after()` method do and when is it called? – jfriend00 May 23 '15 at 16:28
  • Listening for the `ready` event with `.on()` is deprecated starting with jQuery 1.8. The supported forms of listening for that event are listed [here](https://api.jquery.com/ready/). `$(document).ready()` is one of the supported forms. – jfriend00 May 23 '15 at 16:30
  • Please have a look at https://github.com/flightjs/flight to see what I am asking about. Edited the question for clarity. – Rudi May 23 '15 at 16:31

1 Answers1

1

Rudi, it kind of depends on what you're trying to do. A Flight component, typically, is going to attach to a DOM element on initialization. So, if the element exists in the document statically and your script to initialize components is included at the end of the body, you are probably ok.

But, typically, when I'm writing Flight apps, I'll typically have a script or module that initializes all the components for that page and attaches them. In that script, you could wrap it to wait for DOMContentReady, especially if you have code that will dynamically write elements to the page that components will attach to after page load.

If the circumstances in that component need it to listen to DOMContentReady, that's also a useful way to do it int he after('initialize',...) method as well.

David Atchley
  • 1,204
  • 8
  • 10