ijunaidfarooq

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

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

ijunaidfarooq

Thanks a ton.. For all your efforts I learned a lot thank You so much :slight_smile:

DanCouper

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

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement