phoebe

phoebe

jQuery in Phoenix 1.4 / Webpack

Hi,

Following the Programming Phoenix 1.4 beta book, I’d like to use a simple jQuery selector in my form.html.eex template. I have a few questions as to what is the best approach for this:

  1. In which folder is it best to save the js file. If I have a Context/Model, e.g. Multimedia/Video - and a form.html.eex template for Video, where should I save the js code needed for this page? I prefer to save the js code near to the page on which it will be used, so I would have it in a folder such as templates/video/js but it seems that I have to put them in assets/js for webpack to know where to look. Is it common practice out there to do this?

  2. In anycase, my templates cannot find the jquery $ even though in my app.js I have this imported. I have put a simple <script> tag in the form.html.eex template which will use the $ jQuery selector, and I get an error saying $ is not defined - why is this? I thought $ was global when copied to priv/static/js/app.js?

Thanks

Most Liked

kokolegorille

kokolegorille

If You look at the application layout, the link to app.js is at the very end… which is usual for better loading performance. But the template are defined above, so they don’t know about $, because it will be defined later.

One solution I use is to put a placeholder for script just after loading app.js, like this…

<script type="text/javascript" src="<%= Routes.static_url(@conn, "/js/app.js") %>"></script>
<%= render_existing @view_module, "scripts.html", assigns %>

And in the corresponding view, I put

  def render("scripts.html", _assigns) do
    ~E(<script src="/js/page_tree.js"></script>)
  end

Scripts loaded this way have access to $.

But if You want to put javascript code inside template, You would probably need another solution.

It is not tested, but I think I would do it this way…

  • Split bundle in vendor.js and app.js, putting jquery and related into vendor.
  • Load vendor before rendering template.

By the way I use provide plugin in webpack config to load jquery.

    new Webpack.ProvidePlugin({ // inject ES5 modules as global vars
      $: 'jquery',
      jQuery: 'jquery', 'window.jQuery': 'jquery',
      Popper: ['popper.js', 'default']
    })

PS. One quick test would be to move the loading of app.js before rendering template…

kokolegorille

kokolegorille

I put this file under /assets/static/js/

This way it will copied automatically to priv… (by CopyWebpackPlugin)

new CopyWebpackPlugin([{ from: 'static/', to: './' }]),

I don’t need to declare page_tree.js in webpack, but if it is declared after app.js, it will benefit of all the stuff You loaded in app.js.

PS. You can also define multiple entries in webpack.config.js, and use [name].js as output… This would bundle any js through webpack.

OvermindDL1

OvermindDL1

For note, at least Chrome (and I think Firefox and Edge too) all define $ if not otherwise defined and it works like a selector query from jQuery, a convenient helper.

To see if $ is actually defined, test is on window.$ instead.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
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
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

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
JeremM34
Hello, how can I check the Phoenix version ? 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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement