Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
aelaa Avatar asked Jan 17 '26 20:01

aelaa


1 Answers

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.

like image 122
numbers1311407 Avatar answered Jan 19 '26 13:01

numbers1311407



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!