1

If we have multiple files defining widget classes, and if widgets have e.g

class W1(forms.DateInput):

    class Media:
        css = {'all':('common/css/ui-darkness/jquery-ui-1.8.9.custom.css',),}
        js = ('common/js/jquery-ui-1.8.9.custom.min.js',)

class W2(forms.TextArea):

    class Media:
        js = ('common/js/jquery-ui-1.8.9.custom.min.js',)

and we use both W2 and W1 on the same page, that would not be good. I would like to ask what is a possible solution to manage the widgets' media requirements/classes such that I can ensure using multiple widgets would not have duplicate js or css appearing more than once?

Community
  • 1
  • 1
goh
  • 27,631
  • 28
  • 89
  • 151

3 Answers3

3

That linked question is about including JS or CSS manually - where including it twice would indeed be a waste. But, the whole point of the form/widget Media class is to manage exactly this situation: it de-duplicates the references, so each asset is only requested once.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

I'm currently not using Media class, i write inline js with some selfcheck

<script>
    if(typeof(jQuery)!="function"){
        document.write('<script src="//code.jquery.com/jquery-1.10.0.min.js"></' + 'script>');
    }
</script>
James
  • 13,571
  • 6
  • 61
  • 83
0

You can create a metaclass that will analyze all the field widgets for duplicate media resources on the class before a resulting type is created. This is a one-time cost, so any solution will work.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114