如何在Django管理中提供$到第三方的外部jQuery插件 [英] How to provide $ to third-party, external jQuery plugins in Django admin

查看:367
本文介绍了如何在Django管理中提供$到第三方的外部jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于我自己的代码,我已经在我的Django管理员基础模板中添加了几个第三方jQuery插件,我一直很高兴只要做

 (function($){
my_code ='here';
})(django.jQuery);

但是我如何向位于外部文件的其他人的代码提供$?

 < script src ={{STATIC_URL}} js / jquery.json-2.2.min.jstype =text / javascript >< /脚本> 

抱怨说$未定义。我试图把

 < script type =text / javascript> var $ = django.jQuery;< ; /脚本> 

之前的外部引用,但无济于事(btw,为什么呢? ,但执行?我可以在定义之后立即使用$)。



我对Django管理员提供的jQuery版本感到满意,真的不想加载另一个。我也不想编辑别人的插件,以便从上面的$重新定义开始。
编辑:我也不想像我自己的代码一样包装它,我根本不想触摸这些文件。



我真的有要求$ .getScript() - http://api.jquery.com/jQuery.getScript - 进入我的匿名函数来加载这样的文件?



编辑:实际查看外部文件jquery.json-2.2.min.js后,我看到它是已经包装成一个假定jQuery可用的功能,而不是$。插入


var
jQuery = django.jQuery;

在外部引用之前,它工作正常。但是这真的是这样应该做的吗?

解决方案

覆盖django的静态文件 admin / js / jquery。 init.js 通过在应用程序的staticfiles目录中创建一个名称和路径相同的文件。



原始内容是这个

  / *使用noConflict将包含的jQuery放入我们自己的命名空间,并传递
* it'true'。这确保包含的jQuery不会污染全局
*命名空间(即,这将保留两个窗口的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
* /
var django = {
jQuery:jQuery.noConflict(true)
};

只需删除 .noConflict(true)


I've included a couple of third-party jQuery plugins in my Django admin base template which assume "$" to be available.

For my own code, I've always been happy to just do

(function($) {
    my_code = 'here';
})(django.jQuery);

but how can I provide "$" to other people's code which sits in external files?

<script src="{{ STATIC_URL }}js/jquery.json-2.2.min.js" type="text/javascript"></script>

complains that "$" is undefined. I've tried to put

<script type="text/javascript">var $ = django.jQuery;</script>

before that external reference, but to no avail (btw, why is that? I understand loading happens concurrently, but execution? I can use that "$" immediately after defining it.).

I'm happy with the jQuery version that Django admin provides and really don't want to load another one. I also don't want to edit someone else's plugin so that it starts with the above "$" re-definition. EDIT: Neither do I want to wrap it like my own code, I just don't want to touch those files at all.

Do I really have to resort to putting $.getScript() - http://api.jquery.com/jQuery.getScript - into my anonymous function to load such files?

EDIT: After actually looking into that external file jquery.json-2.2.min.js, I saw it was already wrapped into a function that assumed "jQuery" to be available, rather than "$". After inserting

var jQuery = django.jQuery;

before the external reference, it worked fine. But is this really how this should be done?

解决方案

Override django's staticfile admin/js/jquery.init.js by creating a file with the same name and path in your app's staticfiles directory.

The original content is this

/* Puts the included jQuery into our own namespace using noConflict and passing
 * it 'true'. This ensures that the included jQuery doesn't pollute the global
 * namespace (i.e. this preserves pre-existing values for both window.$ and
 * window.jQuery).
 */
var django = {
    "jQuery": jQuery.noConflict(true)
};

Just remove the .noConflict(true).

这篇关于如何在Django管理中提供$到第三方的外部jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆