django-autocomplete-light icon indicating copy to clipboard operation
django-autocomplete-light copied to clipboard

'Use proper selector in DOMElementInserted' prevents dynamic formset select2 initialization

Open dwasyl opened this issue 5 years ago • 4 comments

I'm using DAL 3.3.2 (having sorted out my jQuery issues), however when I added a new dynamic formset the select2 initialization would never run. After spending quite a bit of time debugging a new project running DAL 3.3.2 vs an old one on the 3.2 branch, I found the change in https://github.com/yourlabs/django-autocomplete-light/commit/c799336c15e97638878a6c645ff33e34eb4d6bd8#diff-c969638e50e1f64a208ba1a92bb48afb was the reason for the problem.

When I reversed the change the formset initialization worked great. Perhaps django-dynamic-formset's js renames the elements once they have been inserted (rather than before)? Not entirely sure, if this change was the blocking issue.

Is there a chance that this change could be reversed? With this fixed DAL works perfectly with dynamic-formsets (contrary to our discussion in #327).

dwasyl avatar Jan 11 '19 06:01 dwasyl

The drama about django-dynamic-formset is that it doesn't use the same system as the admin: the admin uses a hidden form with __prefix__ in names IIRC.

As such, reverting does not seem recommendable. However, supporting both would be nice.

jpic avatar Jan 11 '19 09:01 jpic

@jpic dynamic-formset seems to be using the same setup now. It requires a hidden form and all form elements are change to add prefix which is then replaced on DOM insertion.

Supporting both would be nice - although one is just a more specific version of the other (currently $(e.target).find('[data-autocomplete-light-function=select2]:not([id*="__prefix__"])').each(initialize); vs the original $(e.target).find('[data-autocomplete-light-function=select2]').each(initialize);.

From what I can tell, the DOM insertion from dynamic-formset seems to insert the new elements and then change the names? Either way, removing the :not... makes everything work perfectly. Initializing it on the __prefix__ version doesn't seem to do any harm.

dwasyl avatar Jan 29 '19 05:01 dwasyl

Ok to initialize everything if it still passes tox for me (well, apparently tox only ever passed for me anyway :joy:).

jpic avatar Jan 31 '19 04:01 jpic

Okay, I will prepare a PR for this as it's causing me problems and something I've been fixing on my end.

dwasyl avatar Nov 14 '19 08:11 dwasyl