让 jQuery 1.7 和 Require.js(也许还有 Django)合理地发挥作用?
我在使用 jQuery 和 require.js(与 Django 结合使用——开发服务器和 apache)时遇到了非常奇怪的路径错误。
症状是,举个例子,我正在定义依赖于 jQuery 的模块(离开 旧教程)并且 jQuery 被加载多次,最后一次导致 404,例如:
GET localhost:8000/static/js/lib/jquery.js
GET localhost:8000/static/js/lib/backbone.js
GET localhost:8000/js/lib/jQuery-1.7.1.js
Where Django's STATIC_URL='/static/'
,类似的事情发生在如果我尝试使用 Apache 和collectstatic
jQuery 会加载一次(或两次),然后会重新加载,忽略静态设置。
在看到这个其他问题关于命名define
的糟糕程度后,我尝试删除该名称来自 jQuery 的定义调用,所以在我的副本中看起来喜欢this:
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
define([], function () { return jQuery; } );
}
这似乎可行,但感觉很脏。
考虑到所有因素,我可以只使用 jQuery/require.js 捆绑包,但假设它可以很好地处理升级,或者必须处理我的个人配置,我觉得不安全,所以我想知道是否有处理这个混乱的“正确方法”。
I've been having really weird path errors with jQuery and require.js (in combination with Django--both the dev server and apache).
The symptoms were that, as an example, I was defining modules that depended on jQuery (Going off of an old tutorial) and jQuery was being loaded multiple times, with the last time leading to a 404, for example:
GET localhost:8000/static/js/lib/jquery.js
GET localhost:8000/static/js/lib/backbone.js
GET localhost:8000/js/lib/jQuery-1.7.1.js
Where Django's STATIC_URL='/static/'
, and similar things happened where if I tried to use Apache and collectstatic
jQuery would load once (or twice) and then would get re-loaded ignoring the static settings.
After seeing this other question about how named define
's suck, I tried just deleting the name from jQuery's call to define, so in my copy it looks like this:
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
define([], function () { return jQuery; } );
}
This seems to work, but it feels dirty.
All things considered, I could just use the jQuery/require.js bundle, but I don't feel safe assuming that it would handle upgrades well, or necessarily deal with my personal configuration, so I'd like to know if there is a "right way" to handle this mess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论