Skip to content

Commit e907465

Browse files
committed
Allow the observed node to be specified in options.target.
1 parent 7a42d1c commit e907465

4 files changed

Lines changed: 124 additions & 32 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.*.swp
2+
.*.swo

jquery.initialize.js

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// List of MutationSelectorObservers.
2424
var msobservers = [];
2525
msobservers.initialize = function (selector, callback, options) {
26+
var target = options.target || document.documentElement;
2627

2728
// Wrap the callback so that we can ensure that it is only
2829
// called once per element.
@@ -38,38 +39,52 @@
3839
$(selector).each(callbackOnce);
3940

4041
// Then, add it to the list of selector observers.
41-
this.push(new MutationSelectorObserver(selector, callbackOnce, options));
42-
};
42+
var msobserver = new MutationSelectorObserver(selector, callbackOnce, options)
43+
this.push(msobserver);
44+
45+
// The MutationObserver watches for when new elements are added to the DOM.
46+
var observer = new MutationObserver(function (mutations) {
47+
48+
// For each mutation.
49+
for (var m = 0; m < mutations.length; m++) {
50+
console.log(mutations[m]);
51+
52+
// Do we observe this mutation type?
53+
if ($.inArray(mutations[m].type, mtypes) == -1) continue;
54+
55+
if (msobserver.options.scanDocument) {
4356

44-
// The MutationObserver watches for when new elements are added to the DOM.
45-
var observer = new MutationObserver(function (mutations) {
46-
47-
// For each MutationSelectorObserver currently registered.
48-
for (var j = 0; j < msobservers.length; j++) {
49-
if (msobservers[j].options.scanDocument) {
50-
51-
// Scan the entire document.
52-
$(msobservers[j].selector)
53-
.each(msobservers[j].callback);
54-
} else {
55-
56-
// For each mutation.
57-
for (var m = 0; m < mutations.length; m++) {
58-
// If mutation type is observed.
59-
if ($.inArray(mutations[m].type, mtypes) != -1) {
60-
for (var n = 0; n < mutations[m].addedNodes.length; n++) {
61-
$(mutations[m].addedNodes[n]).find(msobservers[j].selector)
62-
.addBack(msobservers[j].selector)
63-
.each(msobservers[j].callback);
64-
}
57+
// Search within the observed node for elements matching the selector.
58+
// This can take longer, but we are more likely to find a match with
59+
// complex selectors.
60+
$(target).find(msobserver.selector)
61+
.each(msobserver.callback);
62+
} else {
63+
64+
// If this is an attributes mutation, then the target is the node upon which the mutation occurred.
65+
if (mutations[m].type == 'attributes') {
66+
$(mutations[m].target)
67+
.find(msobserver.selector) // Find any descendent nodes matching selector
68+
.addBack(msobserver.selector) // Include the mutated node itself.
69+
.each(msobserver.callback); // initialize with the callback.
70+
continue;
71+
}
72+
73+
// Otherwise, search for added nodes.
74+
// Search added nodes only for matching selectors.
75+
for (var n = 0; n < mutations[m].addedNodes.length; n++) {
76+
$(mutations[m].addedNodes[n])
77+
.find(msobserver.selector) // Find any descendent nodes matching selector
78+
.addBack(msobserver.selector) // Include the added node itself.
79+
.each(msobserver.callback); // initialize with the callback.
6580
}
6681
}
6782
}
68-
}
69-
});
83+
});
7084

71-
// Observe the entire document.
72-
observer.observe(document.documentElement, {childList: true, subtree: true, attributes: true});
85+
// Observe the target element.
86+
observer.observe(target, {childList: true, subtree: true, attributes: true});
87+
};
7388

7489
// Deprecated API (does not work with jQuery >= 3.1.1):
7590
$.fn.initialize = function (callback, options) {
@@ -82,7 +97,8 @@
8297
};
8398

8499
$.initialize.defaults = {
85-
scanDocument: true
100+
scanDocument: true,
101+
target: null // Defaults observe the entire document.
86102
}
87103

88104
})(jQuery);

test.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
<h2>We want every .initialize-me item to have color changed to blue by js - no matter how and when item with this class is added</h2>
1313

14+
<button id="add-new">Add new item</button>
15+
16+
<button id="change-class">Just add .initialize-me to .wrong-class</button>
17+
18+
<hr>
19+
1420
<div class="wrong-class">
1521
This elem has .wrong-class and will not be initialized
1622
</div>
@@ -19,10 +25,6 @@ <h2>We want every .initialize-me item to have color changed to blue by js - no m
1925
This class has .initialize-me class so it will be initialized
2026
</div>
2127

22-
<button id="add-new">Add new item</button>
23-
24-
<button id="change-class">Just add .initialize-me to .wrong-class</button>
25-
2628
<p>You can even add item with .initialize-me class via browser inspector - proper js will be executed on it just when you finish edition.</p>
2729

2830
<script>

test2.html

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>jquery.initialize test</title>
6+
<!-- Load MutationObserver and WeakMap polyfill for IE9 and 10 -->
7+
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
8+
<script src="jquery.initialize.js"></script>
9+
</head>
10+
<body>
11+
12+
<h2>We want every .initialize-me item to have color changed to blue by js - no matter how and when item with this class is added</h2>
13+
14+
<button id="add-new">Add new item</button>
15+
16+
<button id="change-class">Just add .initialize-me to .wrong-class</button>
17+
18+
<button id="add-outside">Add new item outside target element.</button>
19+
20+
<hr>
21+
22+
<p>You can even add item with .initialize-me class via browser inspector - proper js will be executed on it just when you finish edition.</p>
23+
24+
<div id="target-element" style="border: 1px dashed red; margin: 15px;">
25+
<div class="wrong-class">
26+
This elem has .wrong-class and will not be initialized
27+
</div>
28+
29+
<div class="initialize-me">
30+
This class has .initialize-me class so it will be initialized
31+
</div>
32+
</div>
33+
34+
<script>
35+
36+
$(function() {
37+
38+
// Deprecated API (does not work with jQuery >= 3.1.1):
39+
// $('.initialize-me').initialize(function() {
40+
// $(this).css('color', 'blue');
41+
// });
42+
43+
$.initialize.defaults.scanDocument = false;
44+
$.initialize.defaults.target = document.getElementById('target-element');
45+
$.initialize('.initialize-me', function() {
46+
$(this).css('color', 'blue');
47+
});
48+
49+
$('#add-new').click(function(){
50+
$('<div>')
51+
.addClass('initialize-me')
52+
.text('New item that was just appended to the target element and it’s color is automatically changed to blue without any additional js.')
53+
.appendTo('#target-element');
54+
});
55+
56+
$('#add-outside').click(function(){
57+
$('<div>')
58+
.addClass('initialize-me')
59+
.text('This item wont be initialised because it was added outside the target element that is being observed.')
60+
.appendTo('body');
61+
});
62+
63+
$('#change-class').click(function(){
64+
$('.wrong-class').addClass('initialize-me');
65+
});
66+
67+
});
68+
69+
</script>
70+
71+
</body>
72+
</html>

0 commit comments

Comments
 (0)