You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
* Fixed incorrect code sample syntax
* Edits
- Updated overview to clarify that the topic describes calling and initializing JS for two different types of files (PHTML and .js)
- Removed some unnecessary sentences
- Cleaned up language and introductions to examples to reduce wordiness and clarify concepts
* Fixed broken links and code syntax
Copy file name to clipboardExpand all lines: guides/v2.1/javascript-dev-guide/javascript/js_init.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -9,39 +9,38 @@ redirect_from:
9
9
10
10
## Overview {#js_init_overview}
11
11
12
-
This topic talks about how to insert a [JavaScript component]({{ site.gdeurl }}javascript-dev-guide/bk-javascript-dev-guide.html#js_terms) in `.phtml` page templates and `.js` files in Magento 2.
12
+
This topic describes different ways to call and initialize JavaScript in Magento 2:
13
13
14
-
It covers declarative notation, used when initialization is required, and imperative notation, used in other cases.
14
+
- Insert a [JavaScript component]({{ site.gdeurl }}javascript-dev-guide/bk-javascript-dev-guide.html#js_terms) in `.phtml` page templates.
15
+
- Call Javascript components that require initialization in Javascript (`.js`) files.
15
16
16
17
We strongly recommend that you use the described approaches and do not add inline {% glossarytooltip 312b4baf-15f7-4968-944e-c814d53de218 %}JavaScript{% endglossarytooltip %}.
17
18
18
19
## Insert a JS component in a PHTML template {#init_phtml}
19
20
20
-
Depending on your task, you might want to use declarative or imperative notation. Both ways are described in the following sections.
21
+
Depending on your task, you can use declarative or imperative notation to insert a JS component into a PHTML template. Use declarative notation if a component requires initialization and imperative notation in other cases.
21
22
22
23
### Declarative notation
23
24
24
-
Using the declarative notation to insert a JS component allows preparing all the configuration on the {% glossarytooltip 74d6d228-34bd-4475-a6f8-0c0f4d6d0d61 %}backend{% endglossarytooltip %} side and outputting it to the page source using standard tools. You should use declarative notation if your JavaScript component requires initialization.
25
+
Using the declarative notation to insert a JS component prepares all the configuration on the {% glossarytooltip 74d6d228-34bd-4475-a6f8-0c0f4d6d0d61 %}backend{% endglossarytooltip %} and outputs it to page source using standard tools. Use declarative notation if your JavaScript component requires initialization.
25
26
26
-
In Magento 2 there are two ways of declarative notation:
27
+
In Magento 2, you have two options for specifying declarative notation:
27
28
- using the `data-mage-init` attribute
28
29
- using the `<script type="text/x-magento-init" />` tag
29
30
30
-
Both ways are described further.
31
-
32
31
#### Declarative notation using the `data-mage-init` attribute {#data_mage_init}
33
32
34
-
Use the `data-mage-init` attribute to insert a JS component in a certain HTML element. The following code sample is an illustration. Here a JS component is inserted in the `<nav/>` element:
33
+
Use the `data-mage-init` attribute to insert a JS component in a specified HTML element. The following example inserts a JS component in the `<nav/>` element:
When inserted in a certain element, the script is called only for this particular element. It is not automatically called for other elements of this type on the page.
39
+
When the Javascript is inserted into the specified element, the script is called only for this particular element. It is not automatically called for other elements of this type on the page.
40
40
41
41
##### How `data-mage-init` is processed {#init_process}
42
42
43
-
On DOM ready, the `data-mage-init` attribute is parsed to extract component names and configuration to be applied to the element.
44
-
Depending on the type of the inserted JS component, processing is performed as follows:
43
+
On DOM ready, the `data-mage-init` attribute is parsed to extract component names and configuration to be applied to the element. Depending on the type of the inserted JS component, processing is performed as follows:
45
44
46
45
- If an object is returned, the initializer tries to find the `<component_name>` key. If the corresponding value is a function, the initializer passes the `config` and `element` values to this function. For example:
47
46
```javascript
@@ -55,17 +54,17 @@ Depending on the type of the inserted JS component, processing is performed as f
55
54
returnfunction(config, element) { ... };
56
55
```
57
56
58
-
- If neither a function nor an object with the `"component_name>"` key are returned, then the initializer tries to search for <code>"<component_name>"</code> in the jQuery prototype. If found, the initializer applies it as `$(element).<component_name>(config)`. For example:
57
+
- If neither a function nor an object with the `"<component_name>"` key are returned, then the initializer tries to search for `"<component_name>"` in the jQuery prototype. If found, the initializer applies it as `$(element).<component_name>;(config)`. For example:
59
58
```javascript
60
-
$.fn.<component_name>=function() { ... };
59
+
$.fn.<component_name>=function() { ... };
61
60
return;
62
61
```
63
62
64
-
- If none of the previous cases is true, the component is executed with no further processing. Such a component does not require either `config` or `element`. The recommended way to declare such components is [using the <script\> tag].
63
+
- If none of the previous cases is true, the component is executed with no further processing. Such a component does not require either `config` or `element`. The recommended way to declare such components is [using the `<script>` tag](#init_script).
65
64
66
-
#### Declarative notation using the `<script type="text/x-magento-init" />` tag {decl_tag}
65
+
#### Declarative notation using the `<script type="text/x-magento-init" />` tag {#decl_tag}
67
66
68
-
To call a JS component on aHTML element without direct access to the element or with no relation to a certain element, use the `<script type="text/x-magento-init">` tag and attribute. Thesyntax is following:
67
+
To call a JS component on anHTML element without direct access to the element or with no relation to a certain element, use the `<script type="text/x-magento-init">` tag and attributesyntax shown in the following example.
69
68
70
69
```html
71
70
<script type="text/x-magento-init">
@@ -84,11 +83,12 @@ To call a JS component on a HTML element without direct access to the element or
84
83
```
85
84
86
85
Where:
86
+
87
87
-`<element_selector>` is a [selector] (in terms of querySelectorAll) for the element on which the following JS components are called.
88
88
-`<js_component1>` and `<js_component2>` are the JS components being initialized on the element with the selector specified as `<element_selector>`.
89
89
-`<js_component3>` is the JS component called with no binding to an element.
90
90
91
-
The following is a working code sample of a widget call using `<script>`. Here the `accordion` and `navigation` widgets are added to the element with the `#main-container` selector, and the `pageCache` script is inserted with no binding to any element.
91
+
The following example provides a working code sample of a widget call using `<script>`. Here the `accordion` and `navigation` widgets are added to the element with the `#main-container` selector, and the `pageCache` script is inserted with no binding to any element.
92
92
93
93
```html
94
94
<script type="text/x-magento-init">
@@ -106,7 +106,7 @@ The following is a working code sample of a widget call using `<script>`. Here t
106
106
107
107
### Imperative notation {#init_script}
108
108
109
-
Imperative notation allows using raw JavaScript code on the pages and executing particular business logic. The notation using the `<script>` tag, without the `type="text/x-magento-init"` attribute, is the imperative notation. The syntax is following:
109
+
Use imperative notation in the PHTML template to include raw JavaScript code on the pages to execute specified business logic. This method uses the `<script>` tag without the `type="text/x-magento-init"` attribute as shown in the following example:
0 commit comments