javascript 参考错误:未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20106760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:43:53  来源:igfitidea点击:

ReferenceError: is not defined

javascriptjqueryruby-on-railsheroku

提问by YvonC

After I deploy my project on Heroku, I get for the following code in Firebug this error:

在 Heroku 上部署我的项目后,我在 Firebug 中得到以下代码这个错误:

ReferenceError: SirTrevor is not defined

参考错误:未定义 SirTrevor

<script type="text/javascript" charset="utf-8">
$(function(){
  var instances = $('.sir-trevor-area'),
  l = instances.length;
while (l>=2) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Video"], defaultType: "Video" });
 l--;
};
while (l>=1) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Embedlylink"], defaultType: "Embedlylink" });
 l--;
};
while (l>=0) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });
 l--;
};
});
</script> 

Firebug highlights this line "

Firebug 突出显示了这一行“

new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });

"

On localhost everything seems fine, no errors. Anyone an idea what the problem could be? Thank you so much for your help! Really appreciated!

在本地主机上一切正常,没有错误。任何人都知道问题可能是什么?非常感谢你的帮助!非常感谢!

UPDATE:

更新:

SirTrevor is defined in sir-trevor.js which is successfully loaded via the Rails asset pipeline in the application.js file.

SirTrevor 在 sir-trevor.js 中定义,它通过 application.js 文件中的 Rails 资产管道成功加载。

This is my application.js file:

这是我的 application.js 文件:

//= require jquery
//= require jquery_ujs
//= require sir-trevor
//= require underscore.js
//= require eventable.js
//= require twitter/bootstrap
//= require jquery.sidr.js
//= require_tree .

Before push to Heroku I do "rake assets:precompile" . On localhost everything seems fine.

在推送到 Heroku 之前,我会执行 "rake assets:precompile" 。在本地主机上一切似乎都很好。

The application.html.erb header:

application.html.erb 标头:

<!DOCTYPE html>
<html>
<head>
  <title>Tt</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet">
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

</head>

回答by YvonC

In order to close the question a short roundup: Thanks to the help of @iamjpg I finally figured it out.

为了结束这个问题,一个简短的综述:感谢@iamjpg 的帮助,我终于弄明白了。

First I eliminated all the javascript from the <body>and put into separate js files. Then I adjusted the hierarchy in the application.js file. Sir-Trevor has dependencies to underscore.js and eventable.js, so they have to be higher than sir-trevor.js .

首先,我从 中删除了所有 javascript<body>并将其放入单独的 js 文件中。然后我在 application.js 文件中调整了层次结构。Sir-Trevor 依赖于 underscore.js 和 eventable.js,因此它们必须高于 sir-trevor.js 。

//= require jquery
//= require underscore.js
//= require eventable.js
//= require twitter/bootstrap
//= require jquery_ujs
//= require sir-trevor
//= require jquery.sidr.js
//= require_tree .