Search code examples
javascriptember.jsember-cli

How do a define a global variable in the ember-cli environment?


I'm in the process of porting my ember app to ember-cli. I have top level function I'm using that's define like var fooBar = function(a) { };.

I moved that to app/utils/foo-bar.js (it was all in js/app.js), and added export default fooBar; to the end of the file.

I think that would be fine if the place I was using it could do an import, but for reasons I don't want to get into, I need defined as a global variable.

But I still want it in app/utils/ and to be able to import it, as I might rewrite the thing causing me to need it to be a global someday.

Is there some way to maybe have a <script> tag in my index.html that looks up the module and assigns it to a global variable?

Edit:

My forBar function that I want to be global needs to reference another variable via an import statement. But in /vendor I can't use define or import.

So just throwing it in /vendor just gives me back the same problem but for a different variable.


Solution

  • Ok, I think I found the answer.

    <script type="text/javascript">
        var fooBar = require(['path/to/foo-bar']).default;
    </script>
    

    What I didn't realize is ember-cli is using requirejs, once I figured that out, I realized this is actually a requirejs question.