为什么将我所有的 jquery 插件放入一个文件中不起作用?

发布于 2024-10-13 12:00:13 字数 1728 浏览 2 评论 0原文

之前,我有这样的情况:

<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/json2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-msdropdown/js/jquery.dd.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/alertbar.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.masonry.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/fileuploader.js" type="text/javascript"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.jeditable.mini.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.growfield2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.placeholder.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.color.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/tipsy/src/javascripts/jquery.tipsy.js"></script>

加载页面时一切正常,并且没有错误。我的朋友告诉我将它们全部合并到 1 个文件中。所以我这样做了:

find somedir/ -name '*.js' -exec cat {} + > ../allplugins.js

现在,当我包含“allplugins.js”时,我遇到了未捕获的类型错误。对象#没有方法“可编辑”

Before, I had this:

<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/json2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-msdropdown/js/jquery.dd.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/alertbar.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.masonry.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/fileuploader.js" type="text/javascript"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.jeditable.mini.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.growfield2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.placeholder.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.color.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/tipsy/src/javascripts/jquery.tipsy.js"></script>

Everything works when I load the page, and there are no errors. My friend told me to merge them all into 1 file. So I did:

find somedir/ -name '*.js' -exec cat {} + > ../allplugins.js

Now, when I include "allplugins.js", I'm getting Uncaught type errors. Object # has not method "editable"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

早乙女 2024-10-20 12:00:13

您应该按照导入 JavaScript 插件的顺序连接它们。

您可以通过将它们列在一个文件(例如plugins.list)中来做到这一点,每个文件名一行。然后使用以下方法构建 allplugins.js:

cat plugins.list | while read jsfile; do
  echo
  echo "//===== `basename "$jsfile"` ====="
  cat "$jsfile"
done > ../allplugins.js

例如,如果您有一个文件 inc.js,

obj = { 'editable': function(){ return 42; } };

其中包含 script.js 尝试使用 inc.js 中定义的对象:

obj.editable()

如果颠倒这两个文件的顺序,您可以看到该 obj 正在使用但尚未定义。

You should concatenate your javascript plugins in the same order that you import them.

You can do that by listing them in a file (say plugins.list), one line for each filename. Then build allplugins.js using:

cat plugins.list | while read jsfile; do
  echo
  echo "//===== `basename "$jsfile"` ====="
  cat "$jsfile"
done > ../allplugins.js

For example, if you have a file inc.js that has

obj = { 'editable': function(){ return 42; } };

And then script.js that tries to use the object defined in inc.js:

obj.editable()

If you reverse the order of those two files, you can see that obj is being used but has not yet been defined.

何其悲哀 2024-10-20 12:00:13

由于依赖于其他文件,合并的文件的顺序不正确。以与之前相同的顺序合并。

The files merged are not in a proper order due to dependency on other. merge with the same order as previously it was working.

凉薄对峙 2024-10-20 12:00:13

编辑抱歉 - 关于使用“>>”的内容而不是“>”可能是不必要的,因为重定向发生在“find”命令的级别,所以它应该可以正常工作。对此感到抱歉。

这个东西是不必要的

应该是:(

find somedir/ -name '*.js' -exec cat {} + >> ../allplugins.js

也就是说,“>>”而不仅仅是“>”。)在前面加上:

rm ../allplugins.js

或达到相同效果的东西,否则每次构建时都会使文件更大。

结束不必要的部分

是,文件将根据可能完全任意的排序进行连接,而它们几乎肯定具有需要特定部分排序的相互依赖性。您应该重命名树中的文件,以便简单的串联命令以正确的顺序执行其操作,或者以正确的顺序显式列出文件以进行串联。您拥有的脚本标签的原始序列将是文件排序的一个很好的起点。

另外,找到你的朋友并向他/她扔一个水气球。

edit sorry — the stuff about using ">>" instead of ">" is probably unnecessary, as the redirection happens at the level of the "find" command so it should work out OK. Sorry about that.

this stuff is unnecessary

Should be:

find somedir/ -name '*.js' -exec cat {} + >> ../allplugins.js

(That is, ">>" instead of just ">".) Precede that with:

rm ../allplugins.js

or something to the same effect, or else each time you build you'll make the file bigger.

end unnecessary part

Another problem you'll face is that the files will be concatenated based on an ordering that is likely to be completely arbitrary, while they almost certainly have interdependencies that call for a particular partial ordering. You should either rename the files in your tree such that that simple concatenation command does its thing in the right order, or else explicitly list the files out in the right order for concatenation. The original sequence of script tags you've got would be a nice starting point for the ordering of your files.

Also, find your friend and throw a water balloon at him/her.

滥情稳全场 2024-10-20 12:00:13

我注意到你用 django 标记了这个问题。您可能对 django-mediasync 感兴趣,它自动处理 js 文件串联你(参见http://pypi.python.org/pypi/ django-mediasync/2.0.0#joined-files):

连接的文件在
MEDIASYNC 字典使用 JOINED。这是一个
将单个媒体映射到一个字典
加入文件的别名。

'已加入': {
'styles/joined.css': ['styles/reset.css','styles/text.css'],
'scripts/joined.js': ['scripts/jquery.js','scripts/processing.js'],
},

JOINED 中列出的文件将是
合并并推送到 S3
别名的名称。单独的 CSS
文件也将被推送到 S3。
别名必须以 .css 或 .js 结尾
为了使内容类型成为
适当设置。

可以使用现有的模板标签
参见加盟媒体。简单地
使用连接的别名作为参数:

{% css_print "joined.css" %}

在本地提供时,模板标签
将为每个呈现一个 HTML 标签
组成连接的文件
文件:

<代码>

<代码>

远程服务时,一个 HTML 标签
将用名称呈现
加入文件:

<代码>

这旨在通过 Amazon S3 之类的方式使用静态内容站点,但它也可以用于仅从本地服务器提供静态媒体,同时自动处理文件联系。

I noticed you tagged this question with django. You may be interested in django-mediasync which automatically handles js file concatenation for you (see http://pypi.python.org/pypi/django-mediasync/2.0.0#joined-files):

Joined files are specified in the
MEDIASYNC dict using JOINED. This is a
dict that maps individual media to an
alias for the joined files.

'JOINED': {
'styles/joined.css': ['styles/reset.css','styles/text.css'],
'scripts/joined.js': ['scripts/jquery.js','scripts/processing.js'],
},

Files listed in JOINED will be
combined and pushed to S3 with the
name of the alias. The individual CSS
files will also be pushed to S3.
Aliases must end in either .css or .js
in order for the content-type to be
set appropriately.

The existing template tags may be used
to refer to the joined media. Simply
use the joined alias as the argument:

{% css_print "joined.css" %}

When served locally, template tags
will render an HTML tag for each of
the files that make up the joined
file:

<link rel="stylesheet"
href="/media/styles/reset.css"
type="text/css" media="screen,
projection" />

<link rel="stylesheet"
href="/media/styles/text.css"
type="text/css" media="screen,
projection" />

When served remotely, one HTML tag
will be rendered with the name of the
joined file:

<link rel="stylesheet"
href="http://bucket.s3.amazonaws.com/styles/joined.css"
type="text/css" media="screen,
projection" />

This is designed to use a static content site via something like Amazon S3, but it could also be used to just serve the static media from your local server, while automagically handling file contactenation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文