jquery.initialize
jquery.initialize copied to clipboard
Dynamic loading execution order
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!
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));
};
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
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)?
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.