0

I'm loading a Bootstrap modal file in HtmlView.php and then a js file to manage it with the following schema:

joomla.asset.json

...
"assets": [
    {
        "name": "almacen.modal_pintura",
        "type": "script",
        "uri": "com_almacen/modal_pintura.js",
        "dependencies": [
            "jquery",
            "core"
        ],
        "attributes": {
            "defer": true
        }
    }
  ]
...

HtmlView.php

...
    public function display($tpl = null)
    {
        ....
        include_once ('components/com_almacen/modal/modal_pintura.php');
        
        $wa = $this->document->getWebAssetManager();
        $wa->useScript('almacen.modal_pintura');
        ....

modal_pintura.php

...
<div class="modal fade" id="modal_pintura"  tabindex="-1" aria-hidden="true">
    ....
</div>

modal_pintura.js

$('#modal_pintura').on('show.bs.modal', function (event) {
    console.log('Event: ', event);
});

js function is loaded and modal open on click a button, but show.bs.modal event doesn't fire. It only works wrapping all js code in a jQuery .ready function:

modified modal_pintura.js

$(document).ready(function() {
    $('#modal_pintura').on('show.bs.modal', function (event) {
        console.log('Event: ', event);
    });
});

Please, Is there any different dependencies or attributes to include in joomla.asset.json for load js file after all html or specific php file?

2
  • 1
    I can't reproduce the issue. Wrapping in jQuery.ready() shouldn't be necessary since you're already using defer attribute.
    – Sharky
    Dec 27, 2023 at 9:27
  • Thanks again,Sharky, you are always there to help us.
    – Peli
    Dec 27, 2023 at 16:23

1 Answer 1

0

Testing and testing, in joomla.asset.json file I have removed jQuery dependency, reordering and I don't know why but finally it works!

joomla.asset.json

...
    {
        "name": "almacen.arandela",
        "type": "script",
        "uri": "com_almacen/arandela.js",
        "attributes": {
            "defer": true
        },
        "dependencies": [
            "core"
        ]
    }
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.