-1
Because it is not possible to import a module after loading an external content by jQuery.load(), from what I understand it needs to be outside the scope, but outside the scope the content of the script is not read, resulting in the error message:
Uncaught Syntaxerror: import declarations may only appear at top level of a module
The module has already been loaded on the page and works in the calls of the existing buttons, but it is not recognized in the content created in the DOM, so I thought it would be possible, but not.
Maybe there is another way to link the function to external content loaded, but what I tried was below resulting in error.
$(document).ready(function(){
$(document).on('click','button.btn-load-items',function( event ) {
event.preventDefault();
let token = $(this).data('category');
$( 'div#load' + token ).load( 'load-items.php?token=' + token, function (e) {
$( 'div.load-images' + token ).hide();
$("html, body").animate({ scrollTop: $('div#load'+token).offset().top - 50 }, 500, function() {
$.getScript("assets/js/velocity.min.js", function() {
console.log('ok');
});
/* ---------------------- AQUI -------------------------- */
import QuantityInput from './assets/vendors/quantity-button/quantity.js';
(function(){
let quantities = document.querySelectorAll('[data-quantity]');
if (quantities instanceof Node) quantities = [quantities];
if (quantities instanceof NodeList) quantities = [].slice.call(quantities);
if (quantities instanceof Array) {
quantities.forEach(div => (div.quantity = new QuantityInput(div, 'Down', 'Up')));
}
})();
});
});
});
});
Only one Obs: if you have any error in the syntax of the module to be loaded it will fall in the
catch
.– user167036
Hi, thanks for your wisdom and good will, you have some reference study on scopes, and could also tell me if, using Babel/webpack this helps to globalize(sic) my codes and better deal with this scenario?
– ElvisP
@Eliseub. so this is quite complex, I don’t know how and the structure of your system but try to use a root/parent context, you are already using the IIFE technique this is important, so you will be able to avoid that your imported modules and methods are exposed globally, then you do something dynamic, use the
import
this way and very powerful, you still use the jQuery that is a hand on the wheel, love very much! In the matter of context/scopes and studies, I always use Google, MDN w W3schools, but MDN has helped me a lot, from a while ago Stackoverflow also...– user167036
continuing, the big key/secret and the set you use, its structure, the skeleton of your system. For years I have been working on a system, after some 3 years repeating many index.js files I saw that I could load the world with just one call
ajax
and an index.js... hahaha, it’s laborious but it’s worth it. See the structure how you can align to reuse methods, make it a private context. All this roughly as I said, is complex, solve your problems with simple scripts always, then focus on security, performance and intelligence/ simplicity... time king! = D– user167036
In the Babel/webpack issue I never used, I can’t tell you, for nothing, I’m happy to help!
– user167036