Skip to content

Commit 4e2a4cb

Browse files
author
Denys Rul
committed
MAGETWO-33932: Add ability to initialize modules by selector
- Remove ability to lookup inside of a specific context.
1 parent 7b168c2 commit 4e2a4cb

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

lib/web/mage/apply/main.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ define([
3333
}
3434

3535
/**
36-
* Searches for elements which has 'data-mage-init' attribute.
36+
* Searches for elements with a 'data-mage-init' attribute.
3737
*
38-
* @param {HTMLElement} [parent=document.body] - Optional node inside of which to perform search.
3938
* @returns {Array} An array of elements with 'data-mage-init' attribute.
4039
*/
41-
function getElems(parent) {
42-
var elems;
40+
function getElems() {
41+
var body = document.body,
42+
elems;
4343

44-
parent = parent || document.body;
45-
46-
elems = parent.querySelectorAll('[' + dataAttr + ']');
44+
elems = body.querySelectorAll('[' + dataAttr + ']');
4745
elems = _.toArray(elems);
4846

49-
if (parent.hasAttribute(dataAttr)) {
50-
elems.unshift(parent);
47+
if (body.hasAttribute(dataAttr)) {
48+
elems.unshift(body);
5149
}
5250

5351
return elems;
@@ -75,15 +73,13 @@ define([
7573
/**
7674
* Initializes components assigned to HTML elements via [data-mage-init].
7775
*
78-
* @param {HTMLElement} [ctx=document.body] - Optional node to search inside.
79-
*
8076
* @example Sample 'data-mage-init' declaration.
8177
* data-mage-init='{"path/to/component": {"foo": "bar"}}'
8278
*/
83-
apply: function (ctx) {
79+
apply: function () {
8480
var virtual = processScripts();
8581

86-
getElems(ctx)
82+
getElems()
8783
.map(getData)
8884
.concat(virtual)
8985
.forEach(function (item) {

lib/web/mage/apply/scripts.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ define([
6464
/**
6565
* Searches for script tags whith a 'text/x-magento-init' type.
6666
*
67-
* @param {HTMLElement} [parent=document.body] - Optional node inside of which to perform search.
6867
* @returns {Array} An array of elements script nodes.
6968
*/
70-
function getNodes(parent) {
69+
function getNodes() {
7170
var elems;
7271

73-
parent = parent || document.body;
74-
7572
elems = document.querySelectorAll(scriptSelector);
7673

7774
return _.toArray(elems);
@@ -97,7 +94,6 @@ define([
9794
* to a 'data-mage-init' attribute of an elemennt found by provided selector.
9895
* Note: All found script nodes will be removed from DOM.
9996
*
100-
* @param {HTMLElement} [ctx=document.body] - Optional node to search inside.
10197
* @returns {Array} An array of components not assigned to the specific element.
10298
*
10399
* @example Sample declaration.
@@ -116,8 +112,8 @@ define([
116112
* }
117113
* }
118114
*/
119-
return function (ctx) {
120-
getNodes(ctx)
115+
return function () {
116+
getNodes()
121117
.map(getNodeData)
122118
.forEach(function (item) {
123119
_.each(item, processElems);

lib/web/mage/mage.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* See COPYING.txt for license details.
44
*/
55
/*jshint eqnull:true browser:true expr:true */
6-
/*global require:true console:true*/
76
(function (root, factory) {
87
'use strict';
98

@@ -91,9 +90,9 @@
9190
/**
9291
* Init components inside of dynamically updated elements
9392
*/
94-
$('body').on('contentUpdated', function (e) {
93+
$('body').on('contentUpdated', function () {
9594
if (mage) {
96-
mage.apply(e.target);
95+
mage.apply();
9796
}
9897
});
9998

0 commit comments

Comments
 (0)