jquery.initialize icon indicating copy to clipboard operation
jquery.initialize copied to clipboard

Dynamic loading execution order

Open RevolvingDCON opened this issue 5 years ago • 2 comments

If content is loaded via ajax / xhr request and added to the page, should the content contain a script, the script is executed before initialize has a chance to watch the newly added elements.

This seems like intended behavior but I'm wondering if there is a recommended workaround to execute initialize before the script in this scenario?

Thanks!

RevolvingDCON avatar Oct 21 '19 17:10 RevolvingDCON

I've solved the above issue by adding the ability to call initialize without instantiating a new observer and instead iterating over existing callbacks and returning a promise.

$.initialize = function (selector, callback, options) {
        if(!callback){
            return new Promise(function(resolve,reject){    
                for(var i in msobservers){
                    if(msobservers[i].selector == selector || !selector){
                        $(selector).each(msobservers[i].callback);
                    }
                }
                resolve();
            });
        }
        return msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options));
    };

RevolvingDCON avatar Oct 21 '19 21:10 RevolvingDCON

I've solved the above issue by adding the ability to call initialize without instantiating a new observer and instead iterating over existing callbacks and returning a promise.

$.initialize = function (selector, callback, options) { if(!callback){ return new Promise(function(resolve,reject){ for(var i in msobservers){ if(msobservers[i].selector == selector || !selector){ $(selector).each(msobservers[i].callback); } } resolve(); }); } return msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options)); };

You should make a pull request @pie6k would accept it

eshimischi avatar Feb 16 '20 09:02 eshimischi

sorry but I don't understand how this can work without initializing msobservers. Also the Promise resolves nothing. Maybe I didn't understand something? Could you explain better the logic (a sample could be perfect)?

radiolondra avatar Feb 04 '23 16:02 radiolondra

If you have a pull request with a test case (perhaps with a test file named test-issue-36.html) with a clear description of what you are fixing, then we can think about including your feature.

Note that we do not recommend this library for new projects. See the README.

bezborodow avatar May 26 '23 17:05 bezborodow