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

Commit ab94a14

Browse files
sathiyaaaBarny Shergoldjeff-matthews
authored
Added an explanation on how to pass data to the script via data-mage-init and to access them in the script. (#8311)
* Initial commit-carousel-example-added * fixed lint errors * fixed some more lint erros * Apply suggestions from code review Co-authored-by: Barny Shergold <barny.shergold@vaimo.com> * review changes * js format fixed * some more review comments * fixed lint errors * fixed lint errors * some more formatting * some more formatting * fixed lint errors * fixed lint errors * empty line added * lint error resolved * Apply suggestions from code review editorial changes from the code review Co-authored-by: Jeff Matthews <matthews.jeffery@gmail.com> * Added space Co-authored-by: Barny Shergold <barny.shergold@vaimo.com> Co-authored-by: Jeff Matthews <matthews.jeffery@gmail.com>
1 parent e4b5d74 commit ab94a14

File tree

1 file changed

+57
-0
lines changed
  • src/guides/v2.3/javascript-dev-guide/javascript

1 file changed

+57
-0
lines changed

src/guides/v2.3/javascript-dev-guide/javascript/js_init.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,63 @@ In Magento 2, you have two options for specifying declarative notation:
3131

3232
> This is used to target either a CSS selector or `*`. If the CSS selector matches multiple HTML elements, the script will run for each matched HTML element. For `*`, no HTML element is selected and the script will run once with the HTML DOM as its target. This method can be implemented from anywhere in the codebase to target any HTML element. This is preferred when direct access to the HTML element is restricted, or when there is no target HTML element.
3333
34+
Consider the example of adding a custom carousel JS:
35+
36+
1. Copy the `<carousel_name>.carousel.js` file to the `app/design/frontend/<package_name>/<theme_name>/web/js/<carousel_name>/` directory.
37+
1. Add your RequireJS module at `app/design/frontend/<package_name>/<theme_name>/web/js/carousel.js`.
38+
39+
```javascript
40+
define(['jquery','<carousel_name>'], function($)
41+
{
42+
return function(config, element)
43+
{
44+
$(element).<carousel_name>(config);
45+
};
46+
});
47+
48+
1. Add the RequireJS config to the `app/design/frontend/<package_name>/<theme_name>/requirejs-config.js` file.
49+
50+
```javascript
51+
var config = {
52+
map: {
53+
'*': {
54+
'carousel': 'js/carousel',
55+
'<carousel_name>': 'js/<carousel_name>/<carousel_name>.carousel'
56+
}
57+
}
58+
};
59+
```
60+
61+
You now have two options for specifying declarative notation:
62+
63+
- Use the `data-mage-init` attribute to insert the carousel in a certain element:
64+
65+
```html
66+
<div data-mage-init='{"carousel":{"option": value}}'>
67+
<div class="item">Item 1</div>
68+
...
69+
<div class="item">Item n</div>
70+
</div>
71+
```
72+
73+
- Use with `<script type="text/x-magento-init"/>`:
74+
75+
```html
76+
<div id="<carousel_name>" class="carousel">
77+
<div class="item">Item 1</div>
78+
...
79+
<div class="item">Item n</div>
80+
</div>
81+
82+
<script type="text/x-magento-init">
83+
{
84+
"#<carousel_name>": {
85+
"carousel": {"option": value}
86+
}
87+
}
88+
</script>
89+
```
90+
3491
#### Declarative notation using the `data-mage-init` attribute {#data_mage_init}
3592

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

0 commit comments

Comments
 (0)