seeking some advises. My Magento-2 project has 2 custom JS files that I would like replicated and loaded externally on the site. This arrangement is for our client to use them to add his own code changes and deploy them directly to the production server whenever he makes minor changes in these JS files. Just to give you an example, he wants to change the logo every holiday for which he can deploy these JS files as soon once the logo is changed and stash this file once the holiday is over. All I need to know is what location is ideal for saving external files and how to load and execute them on the Magento site using some function? How do I invoke this?
1 Answer
Try this way
app/code/VendoreName/ModuleName/view/frontend
requirejs-config.js
var config = {
map: {
'*':{
custom_js:'VendoreName_ModuleName/js/custom_js',
}
},
shim:{
'custom_js':{
deps: ['jquery']
},
}
};
app/code/VendoreName/ModuleName/view/frontend/web/js
custom_js.js
define(
[
"jquery",
"domReady!",
"custom_js",
],
function($, dom, custom_js){
$(document).on('click', '.list-product', function(event){
console.log("Call custom Js");
});
})
Add this into your phtml file
<button type="button" class="list-product" data-mage-init='{"custom_js":{}}' >Click Me!</button>
Note: above code is for frontend only.
I Hope This Helps You.