layout | group | subgroup | title | menu_order | menu_title | version | github_link | redirect_from | ||
---|---|---|---|---|---|---|---|---|---|---|
default |
jsdg |
1_Javascript |
Use custom JavaScript |
3 |
Use custom JavaScript |
2.0 |
javascript-dev-guide/javascript/custom_js.md |
|
We strongly recommend not changing the source code of default Magento components and widgets. All customizations must be implemented in custom modules or themes.
To use a custom implementation of an existing Magento JS component:
- Place the custom component source file in one of the following locations:
- Your theme JS files:
<theme_dir>/web/js
- Your module view JS files:
<module_dir>/view/frontend/web/js
- Your theme JS files:
- Create a RequireJS configuration file
requirejs-config.js
, having specified the following:{% highlight JavaScript %} var config = { "map": { "*": { "<default_component>": "<custom_component>" } } };
{% endhighlight %}
Where the following notation is used:
<default_component>
: the name of the default component you replace<custom_component>
: the name of the custom component
For example, if you want to use custom
navigation-menu.js
script instead of the default menu widgets, yourrequirejs-config.js
should contain the following: {% highlight JavaScript %} var config = { "map": { "*": { "menu": "js/navigation-menu", "mage/backend/menu": "js/navigation-menu", } } }; {% endhighlight %} - 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):- Your theme files:
<theme_dir>
- Your module view files:
<module_dir>/view/frontend
- Your theme files:
This way your custom JS component is used instead of the Magento component in all entries all over the frontend area.
You can add a custom JS component/widget, which will extend a default Magento component/widget.To extend a default Magento jQuery widget, create <your_widget_name>.js
with the following contents:
{% highlight JavaScript %} define([ 'jquery', 'jquery/ui', 'mage/<widget.name>' // usually widget can be found in /lib/web/mage dir ], function($){
return $.<your_namespace>.<your_widget_name>; }); {% endhighlight %}
Where the following notation is used:
<your_namespace>.<your_widget_name>
- the name of your custom widget. According to the jQuery widgets naming convention, must contain a namespace and name.mage.<widget.name>
- the name of the Magento widget that you extend.
For information about how to initialize your custom widget in a .phtml
template, see the JavaScript initialization topic.
To extend a default JS Ui component, your custom script must contain the following:
{% highlight JavaScript %} define([ '<component_path>' ], function(<component_alias>){
return <component_alias>.extend({
defaults: { ... }, // properties with default values
... // methods of your component
}); }); {% endhighlight %}
Where the following notation is used:
<component_path>
: path to the default component that you extend<component_alias>
: variable containing the default component that you extend
For example, Filters.js
script extends the default filters.js
:
{% highlight JavaScript %} define([ 'Magento_Ui/js/grid/filters/filters' ], function(Filters){
return Filters.extend({
defaults: { ... }, // properties with default values
... // methods of your component
}); }); {% endhighlight %}
For information about how to initialize your custom JS component in a .phtml
template, see the JavaScript initialization topic.
To disable the auto-loading of default Magento JS components and widget initialization:
- Create a
requirejs-config.js
file with the following content: {% highlight JavaScript %} var config = { deps: [ ] }; {% endhighlight %} - Put the
requirejs-config.js
file in one of the following locations:- Your custom theme files:
<theme_dir>
- Your custom module files:
<module_dir>/view/frontend
- Your custom theme files:
{% highlight JavaScript %} $(mage.apply); {% endhighlight %}
[Configure JavaScript resources]({{ page.baseurl }}javascript-dev-guide/javascript/js-resources.html)