Permalink
Browse files

BUGFIX: Sometimes we were triggering onremove on elements before they…

… had been added to a DOM at all
  • Loading branch information...
1 parent 79def8c commit ca22d2b88771cf3c65e65288c4991050a42883a3 Hamish Friedlander committed Jun 14, 2012
Showing with 60 additions and 12 deletions.
  1. +20 −4 dist/jquery.concrete-dist.js
  2. +20 −4 dist/jquery.entwine-dist.js
  3. +20 −4 src/domevents/jquery.entwine.domevents.addrem.js
@@ -1258,14 +1258,30 @@ catch (e) {
// If this is true, we've changed something to call cleanData so that we can catch the elements, but we don't
// want to call the underlying original $.cleanData
var supressActualClean = false;
- var removed = false;
// Monkey patch $.cleanData to catch element removal
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
- var event = $.Event('EntwineElementsRemoved');
- event.targets = elems
- $(document).triggerHandler(event);
+ // By default we can assume all elements passed are legitimately being removeed
+ var removed = elems;
+
+ // Except if we're supressing actual clean - we might be being called by jQuery "being careful" about detaching nodes
+ // before attaching them. So we need to check to make sure these nodes currently are in a document
+ if (supressActualClean) {
+ var i = 0, len = elems.length, removed = [], ri = 0;
+ for(; i < len; i++) {
+ var node = elems[i], current = node;
+ while (current = current.parentNode) {
+ if (current.nodeType == 9) { removed[ri++] = node; break; }
+ }
+ }
+ }
+
+ if (removed.length) {
+ var event = $.Event('EntwineElementsRemoved');
+ event.targets = removed;
+ $(document).triggerHandler(event);
+ }
if (!supressActualClean) _cleanData.apply(this, arguments);
}
@@ -1258,14 +1258,30 @@ catch (e) {
// If this is true, we've changed something to call cleanData so that we can catch the elements, but we don't
// want to call the underlying original $.cleanData
var supressActualClean = false;
- var removed = false;
// Monkey patch $.cleanData to catch element removal
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
- var event = $.Event('EntwineElementsRemoved');
- event.targets = elems
- $(document).triggerHandler(event);
+ // By default we can assume all elements passed are legitimately being removeed
+ var removed = elems;
+
+ // Except if we're supressing actual clean - we might be being called by jQuery "being careful" about detaching nodes
+ // before attaching them. So we need to check to make sure these nodes currently are in a document
+ if (supressActualClean) {
+ var i = 0, len = elems.length, removed = [], ri = 0;
+ for(; i < len; i++) {
+ var node = elems[i], current = node;
+ while (current = current.parentNode) {
+ if (current.nodeType == 9) { removed[ri++] = node; break; }
+ }
+ }
+ }
+
+ if (removed.length) {
+ var event = $.Event('EntwineElementsRemoved');
+ event.targets = removed;
+ $(document).triggerHandler(event);
+ }
if (!supressActualClean) _cleanData.apply(this, arguments);
}
@@ -82,14 +82,30 @@
// If this is true, we've changed something to call cleanData so that we can catch the elements, but we don't
// want to call the underlying original $.cleanData
var supressActualClean = false;
- var removed = false;
// Monkey patch $.cleanData to catch element removal
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
- var event = $.Event('EntwineElementsRemoved');
- event.targets = elems
- $(document).triggerHandler(event);
+ // By default we can assume all elements passed are legitimately being removeed
+ var removed = elems;
+
+ // Except if we're supressing actual clean - we might be being called by jQuery "being careful" about detaching nodes
+ // before attaching them. So we need to check to make sure these nodes currently are in a document
+ if (supressActualClean) {
+ var i = 0, len = elems.length, removed = [], ri = 0;
+ for(; i < len; i++) {
+ var node = elems[i], current = node;
+ while (current = current.parentNode) {
+ if (current.nodeType == 9) { removed[ri++] = node; break; }
+ }
+ }
+ }
+
+ if (removed.length) {
+ var event = $.Event('EntwineElementsRemoved');
+ event.targets = removed;
+ $(document).triggerHandler(event);
+ }
if (!supressActualClean) _cleanData.apply(this, arguments);
}

0 comments on commit ca22d2b

Please sign in to comment.