KonnorRogers/mrujs

How to deal with removed attributes

KonnorRogers opened this issue · 7 comments

The mutationObserver now properly checks for changed attributes, but what happens if attributes are removed? Should they be rechecked for their validity and have their event listeners removed? If so, how do we make this happen? Do we create a Set of all watched nodes and run through them and make sure theyre correct? this is an...interesting...problem to say the least.

Perhaps another filter could be added to the src/utils/dom.ts to check the validity of the node[s] whos attributes were removed?

lol, I noticed this behavior when working on #66 Went to log the issue and I wound up here.

Would it be possible to add code to the various observerCallback functions that removes the behaviors if the node doesn't match the designated attributes?

e.g. in method.ts:

  observerCallback (nodeList: Node[]): void {
    nodeList.forEach((node) => {
      if (match(node, window.mrujs.querySelectors.linkClickSelector)) {
        node.addEventListener('click', this.handle)
      } else { // <---------------- THIS IS THE NEW PART
       node.removeEventListener('click', this.handle)
     }

    // ...

  }

This may not be the worst way to handle it....I know me and @leastbad have been mulling over "in what scenario would someone ever do this?"

Just to throw an idea on the pile:

You could introduce a method that people could call which would fully de-mrujs an element. This would allow you to sleep well at night while Officially Not Worrying about people randomly disabling parts of an mrujs-blessed element. Programmatically removing the data-remote attribute is just such a weird way to solve a problem.

Hmmm....this seems the most sane. Allow you to remove certain pieces of functionality via JS...I like it!

I agree this is a weird case to design for- I've certainly never done it in all my years of working with OG UJS. A de-mrujs method sounds good to me.

im just gonna leave it alone until someone can come up with a compelling use case. Ive never had this come up before. Marking as closed unless someone can provide a real life example.