Friday, February 20, 2015

Re: Handling FormSet client side with javascript

Hello,

Yes {{ formset.media }} works. But the problem is
wider. How to include/manage the javascript around
the formset?

I want to add a media file (js) only on formset and not
inside a form contained in a formset.

Let me know if I'm not clear in my first message (it's
a bit long to read, I know =D).

Thanks

On 02/19/2015 07:28 PM, Collin Anderson wrote:
Hi,

Sorry for the late reply. Does simply using {{ formset.media }} work?

Collin

On Friday, February 13, 2015 at 11:02:31 AM UTC-5, aRkadeFR wrote:
Hello everyone,

I'm using FormSet in order to add multiple object at once on
a view. To have a more user friendly approach, I created a
'my.formset.js' file and I need to include a 'jquery.formset.js' lib
to add/remove row of the formset (client side) and edit the
formset management information.

My idea to resolve this problem was to include these two
javascript media file everytime I'm using a formset.

A bit like every media js file for widgets :)

So my first attempt was something like that:
```
class BaseFormSet(BaseInlineFormSet):
     class Media:
         js = ("my.formset.js", "jquery.formset.js", )
```

but this doesnt render these js files with formset.media.render_js().

My second attempt was:
```
formset.media.add_js( ["my.formset.js", "jquery.formset.js", ])
```

My last attempt:
```
form = formset.forms[0]
formset.forms[0].fields[form.fields.keys()[0]].media.add_js(["my.formset.js",
"jquery.formset.js", ])
```

Still not working cause the .media._js is regenerating the media js
files.

The only solution so far I have, provided by @tbaxter, is to
include all my js files in all my application, and initialize/use
the formset/widgets javascript only on certain condition.

I don't like the idea of including my js application wide. It's
gonna overload all my pages for nothing (my js files are
completely standalone) when there is no FormSet.

Can I have your tought on this probleme, and what solution
you have in mind?

Thank you,

No comments:

Post a Comment