Skip to content

Commit 4667c0a

Browse files
author
Denys Rul
committed
MAGETWO-33932: Add ability to initialize modules by selector
- Change script module export object
1 parent 6a91cc0 commit 4667c0a

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

lib/web/mage/apply/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define([
66
'underscore',
77
'jquery',
88
'./scripts'
9-
], function (_, $, scripts) {
9+
], function (_, $, processScripts) {
1010
'use strict';
1111

1212
var dataAttr = 'data-mage-init';
@@ -81,7 +81,7 @@ define([
8181
* data-mage-init='{"path/to/component": {"foo": "bar"}}'
8282
*/
8383
apply: function (ctx) {
84-
var virtual = scripts.process();
84+
var virtual = processScripts();
8585

8686
getElems(ctx)
8787
.map(getData)

lib/web/mage/apply/scripts.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ define([
99
'use strict';
1010

1111
var scriptSelector = 'script[type="text/x-magento-init"]',
12+
dataAttr = 'data-mage-init',
1213
virtual = [];
1314

1415
/**
@@ -31,13 +32,13 @@ define([
3132
* @param {HTMLElement} elem - Element whose data should be modified.
3233
*/
3334
function setData(components, elem) {
34-
var data = elem.getAttribute('data-mage-init');
35+
var data = elem.getAttribute(dataAttr);
3536

3637
data = !!data ? JSON.parse(data) : {};
3738
data = $.extend(true, data, components);
3839
data = JSON.stringify(data);
3940

40-
elem.setAttribute('data-mage-init', data);
41+
elem.setAttribute(dataAttr, data);
4142
}
4243

4344
/**
@@ -91,39 +92,37 @@ define([
9192
return JSON.parse(data);
9293
}
9394

94-
return {
95-
/**
96-
* Parses 'script' tags with a custom type attribute and moves it's data
97-
* to a 'data-mage-init' attribute of an elemennt found by provided selector.
98-
* Note: All found script nodes would be removed from DOM.
99-
*
100-
* @param {HTMLElement} [ctx=document.body] - Optional node to search inside.
101-
* @returns {Array} An array of components not assigned to the specific element.
102-
*
103-
* @example Sample declaration.
104-
* <script type="text/x-magento-init">
105-
* {
106-
* "body": {
107-
* "path/to/component": {"foo": "bar"}
108-
* }
109-
* }
110-
* </script>
111-
*
112-
* @example Providing data without selector.
113-
* {
114-
* "*": {
115-
* "path/to/component": {"bar": "baz"}
116-
* }
117-
* }
118-
*/
119-
process: function (ctx) {
120-
getNodes(ctx)
121-
.map(getNodeData)
122-
.forEach(function (item) {
123-
_.each(item, processElems);
124-
});
125-
126-
return virtual.splice(0, virtual.length);
127-
}
95+
/**
96+
* Parses 'script' tags with a custom type attribute and moves it's data
97+
* to a 'data-mage-init' attribute of an elemennt found by provided selector.
98+
* Note: All found script nodes will be removed from DOM.
99+
*
100+
* @param {HTMLElement} [ctx=document.body] - Optional node to search inside.
101+
* @returns {Array} An array of components not assigned to the specific element.
102+
*
103+
* @example Sample declaration.
104+
* <script type="text/x-magento-init">
105+
* {
106+
* "body": {
107+
* "path/to/component": {"foo": "bar"}
108+
* }
109+
* }
110+
* </script>
111+
*
112+
* @example Providing data without selector.
113+
* {
114+
* "*": {
115+
* "path/to/component": {"bar": "baz"}
116+
* }
117+
* }
118+
*/
119+
return function (ctx) {
120+
getNodes(ctx)
121+
.map(getNodeData)
122+
.forEach(function (item) {
123+
_.each(item, processElems);
124+
});
125+
126+
return virtual.splice(0, virtual.length);
128127
};
129128
});

0 commit comments

Comments
 (0)