Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 7e0c437

Browse files
riconeitzelmeker12
authored andcommitted
fixes wrong escaping (#3281)
* 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
1 parent e22a432 commit 7e0c437

File tree

1 file changed

+18
-18
lines changed
  • guides/v2.1/javascript-dev-guide/javascript

1 file changed

+18
-18
lines changed

guides/v2.1/javascript-dev-guide/javascript/js_init.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,38 @@ redirect_from:
99

1010
## Overview {#js_init_overview}
1111

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:
1313

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.
1516

1617
We strongly recommend that you use the described approaches and do not add inline {% glossarytooltip 312b4baf-15f7-4968-944e-c814d53de218 %}JavaScript{% endglossarytooltip %}.
1718

1819
## Insert a JS component in a PHTML template {#init_phtml}
1920

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.
2122

2223
### Declarative notation
2324

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.
2526

26-
In Magento 2 there are two ways of declarative notation:
27+
In Magento 2, you have two options for specifying declarative notation:
2728
- using the `data-mage-init` attribute
2829
- using the `<script type="text/x-magento-init" />` tag
2930

30-
Both ways are described further.
31-
3231
#### Declarative notation using the `data-mage-init` attribute {#data_mage_init}
3332

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:
34+
3535
```html
3636
<nav data-mage-init='{ "<component_name>": {...} }'></nav>
3737
```
3838

39-
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.
4040

4141
##### How `data-mage-init` is processed {#init_process}
4242

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:
4544

4645
- 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:
4746
```javascript
@@ -55,17 +54,17 @@ Depending on the type of the inserted JS component, processing is performed as f
5554
return function(config, element) { ... };
5655
```
5756

58-
- If neither a function nor an object with the `"component_name&gt;"` key are returned, then the initializer tries to search for <code>"&lt;component_name&gt;"</code> in the jQuery prototype. If found, the initializer applies it as `$(element).&lt;component_name&gt;(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:
5958
```javascript
60-
$.fn.&lt;component_name&gt; = function() { ... };
59+
$.fn.<component_name> = function() { ... };
6160
return;
6261
```
6362

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).
6564

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}
6766

68-
To call a JS component on a HTML 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. The syntax is following:
67+
To call a JS component on an HTML 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 syntax shown in the following example.
6968

7069
```html
7170
<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
8483
```
8584

8685
Where:
86+
8787
- `<element_selector>` is a [selector] (in terms of querySelectorAll) for the element on which the following JS components are called.
8888
- `<js_component1>` and `<js_component2>` are the JS components being initialized on the element with the selector specified as `<element_selector>`.
8989
- `<js_component3>` is the JS component called with no binding to an element.
9090

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.
9292

9393
```html
9494
<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
106106

107107
### Imperative notation {#init_script}
108108

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:
110110

111111
```html
112112
<script>

0 commit comments

Comments
 (0)