0

I have an app that uses HTML, and Coffeescript in the frontend. I recently made it possible to change the language thanks to i18next.

Now I have some buttons that change my window.userLang to the different languages, but the user has to refresh some elements of the app to see it translated.

My problem comes because I need the translations made without refreshing the HTML.

In the app, I use Craftyjs, so what I need to know is how can (if possible) from HTML file, call a function that it's defined in Craftyjs.

The function I want to call is: Crafty.scene("main").

Thanks all!

Sascuash
  • 3,661
  • 10
  • 46
  • 65
  • Coffeescript usually puts all your JS inside an anonymous function. If that's the problem, you should set `Crafty` to a global variable so that you can call it from everywhere. – macool Nov 22 '13 at 14:32
  • Ok, If you answer with the same you just told me, I'll mark it as correct. Just tried what you said and it worked (It was so easy I couldn't see it U_U¿) – Sascuash Nov 22 '13 at 14:43
  • Reading the [Lexical Scope](http://coffeescript.org/#lexical-scope) section of the manual might be useful. – mu is too short Nov 22 '13 at 17:29

1 Answers1

2

Create a global name space defined above where your scripts are included. Then in your javascript you can define the functions you need as fields on that namespace.

<script>
  var MySweetWebApp = {};
</script>
<script src="..."></script>

... Inside JS file ....

MySweetWebApp.Crafty = { ... }

... From Anywhere ...

MySweetWebApp.Crafty.scene('main');
Jeffpowrs
  • 4,430
  • 4
  • 30
  • 49