简体   繁体   English

禁用Rails中的Javascript变量重命名

[英]Disabling Javascript variables renaming in Rails

I'm trying to use tripleflap.js in my app. 我正在尝试在我的应用中使用tripleflap.js。 My view is calling the function: 我的观点是调用函数:

<script type='text/javascript' charset='utf-8'>
    TwitterBird();
</script>

So, in application.js: 所以,在application.js中:

function TwitterBird() {
    var twitterAccount = 'ulmicru';
    var showTweet = false;
    var birdSprite='http://127.0.0.1/birdsprite.png';
    tripleflapInit();
}

Then I'm trying to run it, and Javascript debugger says me: 然后我试图运行它,Javascript调试器说我:

Uncaught ReferenceError: birdSprite is not defined 

In precompiled application.js in /public/assets/ I'm watching this: 在/ public / assets /中预编译的application.js我正在看这个:

function TwitterBird()
{var e="ulmicru",t=!1,n="http://127.0.0.1/birdsprite.png";tripleflapInit()}

It seems Rails precompiler had renamed variables, and tripleflap.js cannot find them. 似乎Rails预编译器已重命名变量,而tripleflap.js无法找到它们。

Is it possible to disable variable renaming in assets? 是否可以禁用资产中的变量重命名?

Try to move the variable definitions outside of the function, or just remove the var from in front of them, like this: 尝试将变量定义移到函数外部,或者只是从它们前面删除var ,如下所示:

function TwitterBird() {
  twitterAccount = 'ulmicru';
  showTweet = false;
  birdSprite='http://127.0.0.1/birdsprite.png';
  tripleflapInit();
}

I googled the tripleflapInit script and took a look at it. 我用Google搜索了tripleflapInit脚本并查看了它。 Basically it defines a bunch of configuration variables directly on the window and expects you to overwrite them; 基本上它直接在窗口上定义了一堆配置变量,并希望你覆盖它们; a pretty kludgy script all in all, but that's beside the point. 总而言之,这是一个非常糟糕的剧本,但这不是重点。

Because you're defining birdSprite inside a function with var , you're actually defining a new variable local to that function. 因为你在一个带有var的函数中定义了birdSprite ,所以你实际上是在为该函数定义一个新的变量。 Even if it was not renamed by the minifier, the window-defined tripleflapInit would still not use it, but rather look to the variable defined on the window. 即使它没有被minifier重命名,窗口定义的tripleflapInit仍然不会使用它,而是查看窗口上定义的变量。

I'm not sure why you're getting the error that birdSprite is not defined, but it's possible that the compiler became confused and removed it, thinking it was unused. 我不确定为什么你会得到birdSprite没有定义的错误,但是编译器可能会混淆并删除它,认为它未被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM