开发者

django duplicate widget requirements

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 w开发者_StackOverflow中文版idgets would not have duplicate js or css appearing more than once?


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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜