applegrew/django-select2 — pull request #127 by rizumu (2014-09-11 21:15:46+00:00)
Title: Formset support

The issue:
* Previously, when using {{ form.empty_form }} the inline js would
automatically be called on the `empty_form`. This made it impossible
to dynamically add inlines to the page because of how
`__prefix__` is duplicated to the new inlines.

The changes:
* This commit wraps all the inline js in a function which is attached
to global `window.django_select2` plugin. On page load only non
`empty_form`'s are initialized with select2, giving the developer the option
to attach to the 'add new inline' click and call the `django_select2`
plugin with the proper inline formset id. I am using this now with
`django-superformset` and dynamically inserted inlines that contain
select2 fields are working as expected. See https://github.com/rizumu/django-select2/compare/formset-support?expand=1#diff-ef58fc8cd3b94144380352baed83bbe5R617

Additionally all the inline js for each formset is identicaly, a
future cleanup could be to only inline the field and form id
variables once per {{ form.media }}
Refs: #65, #49, #32, #109

* First converted `widgets.py` to Unix file type so the diff does not
contain windows line endings. See this diff for the commit without the line endings change: https://github.com/rizumu/django-select2/commit/a563c62d334c7fcfff154d9a9382ae2d42226980

* Since all the inline js is now run post page load, we can put all of
the`django_select2` js libraries at the page bottom with other js assets.
refs #51

* As a bonus, this removes all the js code generation libs from utils with
json.dumps().

* I have not tried formsets with the django admin, however this work
will allow inline formsets support to be added to the admin with
less developer effort than before.

* Not sure what you mean by advanced mode in #124, but we can address that after this is reviewed.

I've run the testapp and everything appears to behave as expected, in addition I'm using Tag fields, Heavy Multiple and Heavy Single Select fields on my project and all are working. This really should not introduce any backwards incompatible changes, but there may certainly be some edge cases not considered, and in any case this step in the right direction for maintainability and future feature support of this project (especially the js bits). Please test this out on your projects to make sure it works with all the same as before.