kubetail-org / loadjs

A tiny async loader / dependency manager for modern browsers (899 bytes)
MIT License
2.57k stars 149 forks source link

success doesn't fire when before returns false #65

Closed wgehner closed 6 years ago

wgehner commented 6 years ago

I am trying a custom dom insert as below. The 'done'/success for 'form' never fires. Please advise.

        loadjs([
            '/assets/js/jquery.jsForm.min.js'
        ], 'form', {
            async: false,
            success: function(){console.log('*form success*') /* doesn't fire */}, 
            error: function(pathsNotFound) {console.log('form error'+pathsNotFound)},
            before: function(path, scriptEl) {
                $('#content-wrapper').append(scriptEl)
                console.log('*before*') /* works */
                return false;
            }
        })
    loadjs.ready('form', function(){
        console.log('*form done*') /* doesn't fire */
    })
amorey commented 6 years ago

I think the issue is with $('#content-wrapper').append(scriptEl). I'm not exactly sure what jQuery is doing under the hood but given that the output of $('#content-wrapper') is a collection maybe jQuery is copying scriptEl in order to perform the operation on each object in the collection. Performing the operation on the raw javascript object works:

$('#content-wrapper')[0].append(scriptEl);

https://jsfiddle.net/muicss/mpnt5jx4/

wgehner commented 6 years ago

Works like a charm, thanks!