meetselva / attrchange

onattrchange listener
GNU General Public License v3.0
207 stars 57 forks source link

IE Causes infinite loop when child element modified #25

Closed greenleafmedia closed 8 years ago

greenleafmedia commented 8 years ago

I have a layout with some nested elements

<div id="parent-element">
    <div class="child-element">
        Foo
    </div>
</div>

If I watch the parent and then modify the child's position, in IE 9 and 10 the child update seems to be bubbling up to the parent which fires the event again. This causes an infinite loop that results in no javascript working on the page.

$(document).ready(function () {
    $("#parent-element").attrchange({
        callback: function(e) {
            $("#parent-element > .child-element").css({
                top: "+=" + 100
            });
        }
    });
});

I've tried using the disconnect/reconnect methods and they work fine in web standards compliant browsers, but they are ignored in the older IE.

$(document).ready(function () {
    $("#parent-element").attrchange({
        callback: function(e) {
            var that = $(e.target);
            that.attrchange('disconnect');
            $("#parent-element > .child-element").css({
                top: "+=" + 100
            });
            that.attrchange('reconnect');
        }
    });
});

Any ideas on how to get IE to either not bubble up into an infinate loop or perhaps disable the event from re-firing. I think I have the disconnect/reconnect syntax correct, but maybe not.

meetselva commented 8 years ago

Could you try event.stopPropagation(); inside the attrchange handler?

https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

greenleafmedia commented 8 years ago

Hmmm.. the MutationRecord that's passed into the handler doesn't seem to have the stopPropagation() method on it. Is there another event I should be able to pull out of that record?

meetselva commented 8 years ago

The mutation observer doesn't follow event model and so no event object.

I think the bubbling actually happens only in old browser that uses DOM mutation events (DOMAttrModified) and not the Mutation Observer. If it is true, then the callback should have the event object and we should be able to stop bubbling using event.stopPropagation();

greenleafmedia commented 8 years ago

Yeah, it's definitely only happening in the older versions of IE. When I print console.log(e); in both IE 9 and IE 10 I'm getting a MutationEvent object though. You're thinking I should be receiving a different event object?

meetselva commented 8 years ago

Could you console.log($(<element>).attrchange("getProperties")["method"])? - This should return one of the following.

propertychange DOMAttrModified Mutation Observer polling

meetselva commented 8 years ago

Please let me know if you had resolved this issue. Thanks!

greenleafmedia commented 8 years ago

Unfortunately, I had to move onto a different solution. IE was just going nuts firing off events for any attribute change on any child (including hover states).