0

I'm trying to load a js file in adminhtml area.

I've created my module and requirejs-config.js file under app/code/vendorName/moduleName/view/adminhtml/web folder:

var config = {

        map: {
            "*": {
                stickerConfig: 'vendorName_moduleName/js/stickerConfig'
            }
        }
    };

I see this piece of code in pub/static/_requirejs/adminhtml/Magento/backend/en_US/requirejs-config.js file.

Then I created my stickerConfig.js in app/code/Mainf/Stickers/view/adminhtml/web/js

When I load backend pages, I don't find my javascript in list of js downloaded by browser.

What's wrong?

  • Read more My js file is not loaded in Magento 2 – Khoa TruongDinh Jan 3 '17 at 16:47
  • 1
    You need to call your custom script in your phtml template. – Khoa TruongDinh Jan 3 '17 at 16:54
  • @KhoaTruongDinh I've already used this method (load js by requirejs) in frontend area and it works fine. This should be the right way to improve page loading performance. I am not able to load js by requirejs in adminhtml area but I find documentation about it. Are you sure of what you're writing? – WaPoNe Jan 4 '17 at 7:54
  • Make sure your template is called in adminhtml area? – Khoa TruongDinh Jan 4 '17 at 8:11
  • 1
    @KhoaTruongDinh: thanks to you advice I've found a solution: I load my js code in an adminhtml template called by adminhtml_system_config_edit.xml layout. – WaPoNe Jan 5 '17 at 14:04
1

I solved in this way..

app/code/vendorName/moduleName/view/adminhtml/layout/adminhtml_system_config_edit.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="footer">
            <block class="vendorName\moduleName\Block\Config" template="vendorName_moduleName::js.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/vendorName/moduleName/view/adminhtml/templates/js.phtml:

<script type="text/javascript">
    require(
        ['jquery'],
        function($) {
            $(function() {
                // My js code
            });
        });
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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