|
| 1 | +--- |
| 2 | +group: javascript-developer-guide |
| 3 | +title: Use custom JavaScript |
| 4 | +--- |
| 5 | + |
| 6 | +This topic discusses how to use custom [JavaScript](https://glossary.magento.com/javascript) components with the components provided by Magento or custom replacement implementations. |
| 7 | + |
| 8 | +We strongly recommend that you do not change the source code of default Magento components and widgets. All customizations must be implemented in custom modules or themes. |
| 9 | + |
| 10 | +## Add a custom JS component {#custom_js_overview} |
| 11 | + |
| 12 | +To add a custom JS component (module), take the following steps: |
| 13 | + |
| 14 | +1. Place the custom component source file in one of the following locations: |
| 15 | + - Your theme JS files: `<theme_dir>/web/js` or `<theme_dir>/<VendorName>_<ModuleName>/web/js`. In this case the component is available in your theme and its [child themes]({{ page.baseurl }}/frontend-dev-guide/themes/theme-inherit.html). |
| 16 | + - Your module view JS files: `<module_dir>/view/frontend/web/js`. In this case, the component is available in all modules and themes (if your module is enabled). |
| 17 | + |
| 18 | +1. Optionally, in the corresponding [module](https://glossary.magento.com/module) or theme, create a `requirejs-config.js` configuration file, if it does not yet exist there and set path for your resource. The RequireJS configuration file can be placed in one of the following locations: |
| 19 | + |
| 20 | + - Your theme: `<theme_dir>` |
| 21 | + - Module within your theme: `<theme_dir>/<module_dir>` |
| 22 | + - Your module (depending on the needed area - **base**, **frontend**, **adminhtml**): `<module_dir>/view/<area>` |
| 23 | + |
| 24 | +## Replace a default JS component {#js_replace} |
| 25 | + |
| 26 | +To use a custom implementation of an existing Magento JS component: |
| 27 | + |
| 28 | +Place the custom component source file in one of the following locations: |
| 29 | + |
| 30 | +- Your theme JS files: `/web/js` |
| 31 | +- Your module view JS files: `<module_dir>/view/frontend/web/js` |
| 32 | + |
| 33 | +Create a RequireJS configuration file `requirejs-config.js`, having specified the following: |
| 34 | + |
| 35 | +```javascript |
| 36 | +var config = { |
| 37 | + "map": { |
| 38 | + "*": { |
| 39 | + "<default_component>": "<custom_component>" |
| 40 | + } |
| 41 | + } |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +- `<default_component>`: the name of the default component you replace |
| 46 | +- `<custom_component>`: the name of the custom component |
| 47 | + |
| 48 | +For example, if you want to use a custom `navigation-menu.js` script instead of the default menu widgets, your `requirejs-config.js` should contain the following: |
| 49 | + |
| 50 | +```javascript |
| 51 | +var config = { |
| 52 | + "map": { |
| 53 | + "*": { |
| 54 | + "menu": "js/navigation-menu", |
| 55 | + "mage/backend/menu": "js/navigation-menu" |
| 56 | + } |
| 57 | + } |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +Place your `requirejs-config.js` file in one of the following directories (according to the location of your custom script, see step 1 of this procedure): |
| 62 | + |
| 63 | +- Your [theme](https://glossary.magento.com/theme) files: `<theme_dir>` |
| 64 | +- Your module view files: `<module_dir>/view/frontend` |
| 65 | + |
| 66 | +This way, your custom JS component is used instead of the [Magento component](https://glossary.magento.com/magento-component) in all entries all over the [frontend](https://glossary.magento.com/frontend) area. |
| 67 | + |
| 68 | +## Extend a default JS component {#extend_js} |
| 69 | + |
| 70 | +You can add a custom JS component/widget, which will extend a default Magento component/widget. |
| 71 | + |
| 72 | +### Extend Magento widget {#extend_js_widget} |
| 73 | + |
| 74 | +To extend a default Magento [jQuery](https://glossary.magento.com/jquery) widget, create `<your_widget_name>.js` with contents similar to the following: |
| 75 | + |
| 76 | +```javascript |
| 77 | +define([ |
| 78 | + 'jquery', |
| 79 | + 'jquery-ui-modules/widget', // use individual jQuery UI component if your widget is for frontend or base areas |
| 80 | + // 'jquery/ui', // use all 'jquery/ui' library if your widget is for adminhtml area |
| 81 | + 'mage/<widget.name>' // usually widget can be found in /lib/web/mage dir |
| 82 | +], function($){ |
| 83 | + |
| 84 | + $.widget('<your_namespace>.<your_widget_name>', $.mage.<widget.name>, { ... }); |
| 85 | + |
| 86 | + return $.<your_namespace>.<your_widget_name>; |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +Where the following notation is used: |
| 91 | + |
| 92 | +- `<your_namespace>.<your_widget_name>` - the name of your custom [widget](https://glossary.magento.com/widget). According to the jQuery widgets naming convention, this value must contain a [namespace](https://glossary.magento.com/namespace) and name. |
| 93 | +- `mage.<widget.name>` - the name of the Magento widget that you extend. |
| 94 | + |
| 95 | +{:.bs-callout .bs-callout-tip} |
| 96 | +All jQuery UI components for frontend and base areas are located in `lib/web/jquery/ui-modules` dir. They can be used in JS widgets by `jquery-ui-modules` path mapping like `jquery-ui-modules/widget` and `jquery-ui-modules/slider`. |
| 97 | +Using individual jQuery UI components instead of the monolithic jQuery UI library improves storefront performance. |
| 98 | + |
| 99 | +For information about initializing your custom widget in a `.phtml` template, see the [JavaScript initialization]({{ page.baseurl }}/javascript-dev-guide/javascript/js_init.html) topic. |
| 100 | + |
| 101 | +### Extend a default Ui component {#extend_js_component} |
| 102 | + |
| 103 | +To extend a default JS Ui component, your custom script must contain the following: |
| 104 | + |
| 105 | +```javascript |
| 106 | +define([ |
| 107 | + '<component_path>' |
| 108 | +], function(<component_alias>){ |
| 109 | + |
| 110 | + return <component_alias>.extend({ |
| 111 | + |
| 112 | + defaults: { ... }, // properties with default values |
| 113 | + ... // methods of your component |
| 114 | + }); |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +Where the following notation is used: |
| 119 | + |
| 120 | +- `<component_path>`: path to the default component that you extend |
| 121 | +- `<component_alias>`: variable containing the default component that you extend |
| 122 | + |
| 123 | +For example, `Filters.js` script extends the default `filters.js`: |
| 124 | + |
| 125 | +```javascript |
| 126 | +define([ |
| 127 | + 'Magento_Ui/js/grid/filters/filters' |
| 128 | +], function(Filters){ |
| 129 | + |
| 130 | + return Filters.extend({ |
| 131 | + |
| 132 | + defaults: { ... }, // properties with default values |
| 133 | + ... // methods of your component |
| 134 | + }); |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +For information about initializing your custom JS component in a `.phtml` template, see the [JavaScript initialization] topic. |
| 139 | + |
| 140 | +## Disable default Magento JS {#disable_default_js} |
| 141 | + |
| 142 | +To disable the auto-loading of default Magento JS components and widget initialization: |
| 143 | + |
| 144 | +1. Create a `requirejs-config.js` file with the following content: |
| 145 | + |
| 146 | + ```javascript |
| 147 | + var config = { deps: [ ] }; |
| 148 | + ``` |
| 149 | + |
| 150 | +1. Put the `requirejs-config.js` file in one of the following |
| 151 | + locations: |
| 152 | + |
| 153 | + - Your custom theme files: `<theme_dir>` |
| 154 | + - Your custom module files: `<module_dir>/view/frontend` |
| 155 | + |
| 156 | +If you need to enable the loading of default Magento JS components and widget initialization on a certain stage, add the following code in your JS script: |
| 157 | + |
| 158 | +```javascript |
| 159 | +$(mage.apply); |
| 160 | +``` |
| 161 | + |
| 162 | +{:.ref-header} |
| 163 | +Related topic |
| 164 | + |
| 165 | +- [JavaScript resources in Magento]({{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html) |
| 166 | +- [About AMD modules and RequireJS]({{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html) |
| 167 | + |
| 168 | +[JavaScript initialization]: {{page.baseurl}}/javascript-dev-guide/javascript/js_init.html |
| 169 | +v2.3/performance-best-practices/advanced-js-bundling.html |
0 commit comments