简体   繁体   中英

Disabling Javascript variables renaming in Rails

I'm trying to use tripleflap.js in my app. My view is calling the function:

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

So, in 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:

Uncaught ReferenceError: birdSprite is not defined 

In precompiled application.js in /public/assets/ I'm watching this:

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.

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:

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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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