1

I create the external js for magento 2 backend like this:

Namespace/Module/view/adminhtml/requirejs-config.js:

 var config = {
    map: {
        '*': {
            regionsForm: 'Namespace_Module/js/regions-form'
        }
    }
};

in Namespace/Module/view/adminhtml/web/js/regions-form.js

    define([
        'jquery',
        'underscore',
        'mage/url',
        'jquery/ui'
        ], function ($, _,urlBuilder) {
        'use strict';
        $(function() { 
           console.log('a'); 
        });
    });

i already delete pub/static and ran setup:upgrade & static:content, but when i inspect element in my browser i cant find my js file, but in pub/static my file is already there.

Edit: I ran deploy command php bin/magento setup:static-content:deploy

  • Have you run deploy command php bin/magento setup:static-content:deploy – Rakesh Jesadiya Feb 17 '17 at 5:10
  • @Rakesh yes i already did that – simple guy Feb 17 '17 at 5:57
1

The reason is that we didn't call the custom script. So, we can call it via template or the head. You can try two cases:

1) We can add directly on the head.

app/code/Namespace/Module/view/adminhtml/layout/default.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Namespace_Module::js/regions-form.js"/>
    </head>
    <body/>
</page>

In this case, remember to change define to require in the custom script.

require([
    'jquery',
    'underscore',
    'mage/url',
    'jquery/ui'
], function ($, _,urlBuilder) {
    'use strict';
    console.log('Here');
    return 1;
});

2) We can call it via template:

app/code/Company/Module/view/adminhtml/templates/test.phtml

<script>
    require(["regionsForm"], function($form){
        //Your code here
    })
</script>
  • i got this error define is not a function at regions-form.js:1 – simple guy Feb 17 '17 at 7:51
  • You add your script on the head or via template? – Khoa TruongDinh Feb 17 '17 at 7:54
  • i added it via template – simple guy Feb 17 '17 at 8:00
  • Okay! Let me check again. – Khoa TruongDinh Feb 17 '17 at 8:00
  • okay, is it requirejs-config.js that caused the problem? – simple guy Feb 17 '17 at 8:12
0

Run commands in this sequence can help you:

sudo php bin/magento setup:upgrade sudo chmod -R 777 var sudo chmod -R 777 pub sudo php bin/magento setup:static-content:deploy sudo php bin/magento cache:flush sudo php bin/magento cache:clean sudo chmod -R 777 var sudo chmod -R 777 pub

it might work if there is any permission related issue.

  • done that, still not working, i think it's not permission because the file is already exist in pub/static – simple guy Feb 17 '17 at 6:49
  • can we use require in regions-form.js instead of define – Pankaj Sharma Feb 17 '17 at 7:01
  • still not working – simple guy Feb 17 '17 at 7:06
  • see here if you are missing something OR you need to add regionsForm in regions-form.js and if it is not working add your module js file in admin layout default.xml file. – Pankaj Sharma Feb 17 '17 at 9:32

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.