jQuery 插件和 Rails 3 Asset Pipeline -- 我做错了什么?
我正在 Rails 3.1 中开发一个 Web 应用程序,完全(我认为)利用 css、图像和 js 的资源管道。当我尝试实现 jQuery 插件时,我遇到了一个非常一致的问题。我能够在一种情况下解决它,但在另一种情况下却无法解决,我正在试图找出关键问题是什么。
本质上,我将加载一个 jQuery 插件,然后在我的 document.ready
方法中调用它,结果发现拉出站点会导致(例如,在 jScrollPane 插件的情况下)
Uncaught TypeError: Object #<Object> has no method 'jScrollPane'
我其他几个插件也收到了同样的错误。我认为情况可能是 jQuery/jQuery-UI 没有在我的插件之前加载,因此它们没有正确实例化,但情况似乎并非如此。必要的脚本位于 app/assets/javascripts/...
在我的 app/assets/application.js
中,我有以下内容:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery.mousewheel
//= require mwheelIntent
//= require jquery.jscrollpane.min
//= require_tree .
生成的 application.js除了错误之外,
看起来是正确的;也就是说,我所期望的一切都在那里。
我做错了什么?我很乐意提供任何必要的附加信息。
I'm working on a web app in Rails 3.1, fully (I think) utilizing the asset pipeline for css, images, and js. I'm running into a pretty consistent issue when I try to implement jQuery plugins. I've been able to solve it in one case, but not in another, and I'm trying to figure out what the key issue is.
Essentially, I'll load a jQuery plugin and then call it in my document.ready
method, only to find that pulling up the site results in (for example, in the case of the jScrollPane plugin)
Uncaught TypeError: Object #<Object> has no method 'jScrollPane'
I have received the same error for several other plugins. I thought the case might be that jQuery/jQuery-UI wasn't being loaded before my plugins, so they weren't instantiated properly, but that doesn't seem to be the case. The necessary scripts are in app/assets/javascripts/...
In my app/assets/application.js
I have the following:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery.mousewheel
//= require mwheelIntent
//= require jquery.jscrollpane.min
//= require_tree .
The resultant application.js
appears to be correct, other than the errors; that is, everything I would expect to be there is there.
What am I doing wrong? I'm happy to provide any additional information necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不经常使用资产管道,那么它的一个问题是,如果您预编译了资产并将其转储到 public/assets/application.js 中,那么预编译的文件可能会将文件破坏为它们是由开发资产管道生成的,因此您包含在
/app/assets/application.js
中的插件将被覆盖。换句话说,您正在使用此代码片段精心构建的先前的、唱歌跳舞的 jquery 对象:
被
public/assets/application.js
中描述 jQuery 对象的已编译代码覆盖>。好吧,当我正在调试的一个页面表现出与此页面非常相似的行为时,这就是让我感到震惊的地方。
One gotcha with the asset pipeline if you're not using it regularly is that if you have assets pre-compiled and dumped into
public/assets/application.js
then it that precompiled file can clobber the files as they're generated by the development asset pipeline, so plugins you've included in/app/assets/application.js
are overwritten.In other words, the previous, all singing, all-dancing jquery object you're meticulously building with this snippet here:
Is overwritten by the already compiled code describing a jQuery object in
public/assets/application.js
.Well, that's what just bit me, when a page I was debugging was exhibiting very similar behaviour to this one.
在 Javascript 上工作的压缩器可能对插件的编码风格很挑剔。
我发现在一些情况下,如果插件缺少结束语,则找不到代码;在最后。如果压缩器认为下一个函数位于前一个块内,则可以重命名下一个函数。
尝试重新排序您的插件,看看是否会更改缺少的方法。这可能会给你线索,告诉你失踪的地方;是,如果这就是问题所在。
Compressors that work on Javascript can be fussy about the coding style of plugins.
I have found in a couple of cases code isn't found if the plugin is missing a closing ; at the end. The compressor can rename the next function if it thinks it is inside the precceding block.
Try reordering your plugins and see if that changes the missing method. That might give you clue as to where the missing ; is, if that is the problem.