shama / on-load

On load/unload events for DOM elements using a MutationObserver
113 stars 19 forks source link

Not being called when toggling between two elements of the same tagName #5

Closed timwis closed 8 years ago

timwis commented 8 years ago

Per yoshuawuyts/choo#1:

hopefully I'm just doing it wrong but I was just trying to implement on-load and ran into unexpected behaviour with it. Perhaps I'm misunderstanding? Here's a simple test case. Shouldn't that work?

It seems like the second page doesn't trigger onload because it uses the same type of parent container (<div>). Even if you give that <div> different id or class, it doesn't trigger the second view. Only if you use a different element type (like <section>).

@yoshuawuyts suggested it may be because of this not being cleaned up. Any advice?

shama commented 8 years ago

Oh interesting. It looks like morphdom doesn't add/remove that node so the mutation observer never fires. It only changes the attribute in the DOM.

I can fix that by also observing the data-onloadid attribute and when one changes into another, fire the onunload and onload for each respectively.

shama commented 8 years ago

@timwis Ok give 2.2.0 a go. It now watches data-onloadid attribute changes in case the node doesn't change but the attribute did. It has some perf improvements too.

timwis commented 8 years ago

woohoo! Looks like the above test case is working as expected now. Thanks for the quick resolution, @shama!