How to mock jquery ready function

Cheok Yan Cheng

Due to certain reason, we are going to remove jquery from our legacy app (Please don't ask why!)

However, there 1000+ of template files by designers, are making used of jquery ready function. We plan to make the following mock strategy.

<head>
<script type="text/javascript">
    // // http://stackoverflow.com/a/9899701
    (function(funcName, baseObj) {
        // The public function name defaults to window.docReady
        // but you can pass in your own object and own function name and those will be used
        // if you want to put them in a different namespace
        funcName = funcName || "docReady";
        baseObj = baseObj || window;
        var readyList = [];
        var readyFired = false;
        var readyEventHandlersInstalled = false;

        // call this when the document is ready
        // this function protects itself against being called more than once
        function ready() {
            if (!readyFired) {
                // this must be set to true before we start calling callbacks
                readyFired = true;
                for (var i = 0; i < readyList.length; i++) {
                    // if a callback here happens to add new ready handlers,
                    // the docReady() function will see that it already fired
                    // and will schedule the callback to run right after
                    // this event loop finishes so all handlers will still execute
                    // in order and no new ones will be added to the readyList
                    // while we are processing the list
                    readyList[i].fn.call(window, readyList[i].ctx);
                }
                // allow any closures held by these functions to free
                readyList = [];
            }
        }

        function readyStateChange() {
            if (document.readyState === "complete") {
                ready();
            }
        }

        // This is the one public interface
        // docReady(fn, context);
        // the context argument is optional - if present, it will be passed
        // as an argument to the callback
        baseObj[funcName] = function(callback, context) {
            // if ready has already fired, then just schedule the callback
            // to fire asynchronously, but right away
            if (readyFired) {
                setTimeout(function() {
                    callback(context);
                }, 1);
                return;
            } else {
                // add the function and context to the list
                readyList.push({
                    fn: callback,
                    ctx: context
                });
            }
            // if document already ready to go, schedule the ready function to run
            if (document.readyState === "complete") {
                setTimeout(ready, 1);
            } else if (!readyEventHandlersInstalled) {
                // otherwise if we don't have event handlers installed, install them
                if (document.addEventListener) {
                    // first choice is DOMContentLoaded event
                    document.addEventListener("DOMContentLoaded", ready, false);
                    // backup is window load event
                    window.addEventListener("load", ready, false);
                } else {
                    // must be IE
                    document.attachEvent("onreadystatechange", readyStateChange);
                    window.attachEvent("onload", ready);
                }
                readyEventHandlersInstalled = true;
            }
        }
    })("docReady", window);

    // Mock jquery.
    var jQuery = function(baseObj) {
        return {
            ready: function(baseObj) {
                docReady(baseObj);
            }
        }
    };
    var $ = jQuery;
</script>

</head>

Take note, we tend to mock jquery ready with the following code snippet.

// Mock jquery.
var jQuery = function (baseObj) {
    return {
        ready: function (baseObj) {
            docReady(baseObj);
        }
    }
};
var $ = jQuery;

It works for cases

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

However, how can we make the following syntax works as well?

$(function() {...});
Tushar

Check if the passed parameter is function

var jQuery = function(baseObj) {
    if (typeof baseObj === 'function')
        return docReady(baseObj);

Code:

// Mock jquery.
var jQuery = function (baseObj) {
    if (typeof baseObj === 'function')
        return docReady(baseObj);

    return {
        ready: function (baseObj) {
            docReady(baseObj);
        }
    }
};
var $ = jQuery;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError: jQuery(...).ready(...) is not a function

From Dev

The document ready function in jQuery

From Dev

The document ready function in jQuery

From Dev

scope in jquery ready function

From Dev

jquery - add a function to `ready()` or call if already `ready()`

From Dev

Angular2 JQuery, how to make the document.ready function

From Dev

How important it is to wrap jquery code in $(document).ready() function?

From Dev

jQuery - How to create a new function to simulate document.ready on ajaxComplete?

From Java

Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

From Dev

How work jQuery ready

From Dev

wkhtmltopdf and the jQuery.ready() function

From Dev

jQuery function ready for new elements

From Dev

jQuery ready function for multiple drawings

From Dev

JQuery: Uncaught TypeError: jQuery(...).ready(...) is not a function

From Dev

How to mock a decorated function

From Dev

How to mock jQuery with Jasmine

From Dev

jQuery.ready() function and asynchronous CSS

From Dev

jquery loaded async and ready function not working

From Dev

jquery $(document).one ('ready', function () {...} does not work

From Dev

jQuery's ready() function works in .jsp, but not in .jspx

From Dev

jQuery document ready and keypress to call one function

From Dev

Using jQuery without document.ready function

From Dev

jQuery.ready() function and asynchronous CSS

From Dev

Understanding Jquery function - $(document).ready(callback);

From Dev

jQuery .ready() function use in other event

From Dev

load a jquery function after docuemnt.ready()

From Dev

how to call function that is inside a function ready()?

From Dev

How to pass parameter to jQuery document.ready() function (jstl, MVC, java)

From Dev

How to call $( document ).ready() jQuery function with in ngAfterViewInit() on Component class using angular 2

Related Related

  1. 1

    TypeError: jQuery(...).ready(...) is not a function

  2. 2

    The document ready function in jQuery

  3. 3

    The document ready function in jQuery

  4. 4

    scope in jquery ready function

  5. 5

    jquery - add a function to `ready()` or call if already `ready()`

  6. 6

    Angular2 JQuery, how to make the document.ready function

  7. 7

    How important it is to wrap jquery code in $(document).ready() function?

  8. 8

    jQuery - How to create a new function to simulate document.ready on ajaxComplete?

  9. 9

    Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

  10. 10

    How work jQuery ready

  11. 11

    wkhtmltopdf and the jQuery.ready() function

  12. 12

    jQuery function ready for new elements

  13. 13

    jQuery ready function for multiple drawings

  14. 14

    JQuery: Uncaught TypeError: jQuery(...).ready(...) is not a function

  15. 15

    How to mock a decorated function

  16. 16

    How to mock jQuery with Jasmine

  17. 17

    jQuery.ready() function and asynchronous CSS

  18. 18

    jquery loaded async and ready function not working

  19. 19

    jquery $(document).one ('ready', function () {...} does not work

  20. 20

    jQuery's ready() function works in .jsp, but not in .jspx

  21. 21

    jQuery document ready and keypress to call one function

  22. 22

    Using jQuery without document.ready function

  23. 23

    jQuery.ready() function and asynchronous CSS

  24. 24

    Understanding Jquery function - $(document).ready(callback);

  25. 25

    jQuery .ready() function use in other event

  26. 26

    load a jquery function after docuemnt.ready()

  27. 27

    how to call function that is inside a function ready()?

  28. 28

    How to pass parameter to jQuery document.ready() function (jstl, MVC, java)

  29. 29

    How to call $( document ).ready() jQuery function with in ngAfterViewInit() on Component class using angular 2

HotTag

Archive