How can I get my jasmine tests fixtures to load before the javascript considers the document to be “ready”?

后端 未结 6 2474
星月不相逢
星月不相逢 2021-01-01 21:51

I am fairly certain that the issue is that the jquery bindings set to run on $(document).ready do not have the fixture html available to them. So when my events occur that

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 22:26

    I used dom_munger to automatically generate the fixture for me, so adding the jquery code manually inside the fixture isn't an option for me. I've been searching for the same answer, and the best solution I can think of right now is to use event delegate in jQuery.

    For example, instead of writing this:

    $('#target').click(function(){
        // Do work
    });
    

    Do this:

    $(document).on('click', '#target', function(){
        // Do work
    });
    

    In this way, the listener is attached to the document, which is present before the fixture is loaded. So when the click event happens on #target, it bubbles up to document. Document checks if the origin matches the second argument - '#target'. If it matches, the handler function will be called.

    I know there are limitations to this, such as initializing plug-ins, but it does solve part of the problem.

提交回复
热议问题