ijunaidfarooq
JS files in Phoenix Project
I have created my Project with -no--brunch, Now I have added a file in priv/static/js as main.js,
What I want to do is: In Rails I only add a file and reference it in application.js and then in that file I use this method
firstOne = ->
console.log("main on main")
window.initializeAdmins = ->
firstOne()
and the in the any of my HTML file i can easily do as
<script>
$(document).ready(function () {
window.initializeAdmins();
});
</script>
And so I can do and use what ever I want to do, Is that possible in Phoenix? without using brunch. I have tried brunch but its giving me strange behaviour as Creating modules and other stuff , Why I cant be simple as it is.
Create a js file, reference it in main js file and then use it anywhere where i want to.
Most Liked
DanCouper
@ijunaidfarooq here is the example project. Writeup is quite long, if anything isn’t clear either put an issue in or ask here, I’ll get it updated. Examples may be a bit silly. Anyway:
https://github.com/DanCouper/brunch_example
Every stage (Scss, CS, globals) has it’s own branch; the main branch (`diy_brunch) just has the description on a default Phoenix install so kinda expects that you pull it down and go through it. There are no major things, just a small number of very small changes.
As @cassiogodinho mentions, doesn’t make an awful lot of sense to use CS over ES6, and you get ES6 out of the box with Phoenix, no setup or config. I’d strongly advise using that, even if it is a little bit less terse; CS doesn’t really have anything bar (arguable) syntax niceties over ES6, and with ES6 you get a whole lot more. Plus a lot more people will be able to understand the code if it’s ES6, so it’s a lot easier to get help. But ¯_(ツ)_/¯
ijunaidfarooq
DanCouper
That should work the same if you manually load the file; if I’ve got this right: you have a function or method in a JS file. It’s global. In an arbitrary HTML file you’re waiting for the document to load so that you can guarantee the function is available. You’re running the function.
Rails does what it does via the Sprockets library; it is importing the JS you referenced in application.js via magic. What you’ve done by saying --no_brunch is remove the equivalent to Sprockets from Phoenix. It can’t load your file automatically because you’ve removed the thing that would allow it; by default, with brunch, you get a main JS file that you can import other JS files into (and it has to be specified explicitly; it doesn’t do magic like Rails). Without brunch, you need to manually import every JS file into the head or foot of your HTML
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









