Django and jQuery, I don't get it

119 views
Skip to first unread message

Olivier Dalang

unread,
Jun 9, 2016, 11:07:01 AM6/9/16
to django...@googlegroups.com
Hi !

I'm using several modules that rely on jQuery, and have some own needs as well :

- admin
- grappelli
- autocomplete light
- session security
...

I also see that there are tons of different variables that reference jQuery, defined by theses modules, some of which are the same, and some of which are not :

$
jQuery
django.jQuery
grp.jQuery
yl.jQuery
...

I also see django uses this type of syntax which seems to deal about dependecies and clonflicts, but is very cryptic to me (unfortunately such a problem is very hard to google) :

/*global DateTimeShortcuts, SelectFilter*/
...
(function($) {
    'use strict';
    ....
})(django.jQuery);


Being a javascript ignorant, up to now, each time I had some javascript exceptions because of jQuery conflicts/undefined, I hacked a bit randomly until the exception disappeared, but I never understood the logic behind this.

I'd love some guidance, as my project grows more and more complex, and hacking randomly doesn't shine as a particularly good long term strategy.

As partical example, what would be a clean way to add jQuery.AreYouSure to the django admin forms ?

Is there some documentation or guidelines that explain best practices ? Or is it indeed a mess and not just me not understanding ?

Thanks !

Olivier


Mike Dewhirst

unread,
Jun 9, 2016, 7:35:24 PM6/9/16
to django...@googlegroups.com
I can't help at all with jquery but IIRC there is a question in the
Admin "Are you sure?" when you try deleting something from a list view.

Might be a start

Mike

On 10/06/2016 1:06 AM, Olivier Dalang wrote:
> Hi !
>
> I'm using several modules that rely on jQuery, and have some own needs
> as well :
>
> - admin
> - grappelli
> - autocomplete light
> - session security
> ...
>
>
> I also see that there are tons of different variables that reference
> jQuery, defined by theses modules, some of which are the same, and some
> of which are not :
>
> $
> jQuery
> django.jQuery
> grp.jQuery
> yl.jQuery
> ...
>
>
> I also see django uses this type of syntax which seems to deal about
> dependecies and clonflicts, but is very cryptic to me (unfortunately
> such a problem is very hard to google) :
>
> /*global DateTimeShortcuts, SelectFilter*/
> ...
> (function($) {
> Â Â 'use strict';
> Â Â ....
> })(django.jQuery);
>
>
>
> Being a javascript ignorant, up to now, each time I had some javascript
> exceptions because of jQuery conflicts/undefined, I hacked a bit
> randomly until the exception disappeared, but I never understood the
> logic behind this.
>
> I'd love some guidance, as my project grows more and more complex, and
> hacking randomly doesn't shine as a particularly good long term strategy.
>
> As partical example, what would be a clean way to add jQuery.AreYouSure
> <https://github.com/codedance/jquery.AreYouSure>Â to the django admin
> forms ?
>
> Is there some documentation or guidelines that explain best practices ?
> Or is it indeed a mess and not just me not understanding ?
>
> Thanks !
>
> Olivier
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAExk7p2eggp9rTviqo%2BQ%3DUhprxoLPJ4kWeP7Z_sx0xjZY%3DSVqA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAExk7p2eggp9rTviqo%2BQ%3DUhprxoLPJ4kWeP7Z_sx0xjZY%3DSVqA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

ludovic coues

unread,
Jun 10, 2016, 4:37:44 AM6/10/16
to django...@googlegroups.com
> (function($) {
> 'use strict';
> ....
> })(django.jQuery);

This syntax is called a closure. First you declare a function so you
don't contaminate the global scope with your local declared variable.
Then you execute your function.
I will assume django.JQuery is the version of JQuery shipped with
django. Inside the closure, you can refer to it as simply the variable
$.

I hope that help you.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAExk7p2eggp9rTviqo%2BQ%3DUhprxoLPJ4kWeP7Z_sx0xjZY%3DSVqA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Stephen J. Butler

unread,
Jun 10, 2016, 9:33:01 AM6/10/16
to django...@googlegroups.com
Specifically, this style is called an Immediately Invoked Function Expression, or IIFE.


Reply all
Reply to author
Forward
0 new messages