phoebe
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:
-
In which folder is it best to save the js file. If I have a Context/Model, e.g.
Multimedia/Video- and aform.html.eextemplate forVideo, 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 astemplates/video/jsbut it seems that I have to put them inassets/jsfor webpack to know where to look. Is it common practice out there to do this? -
In anycase, my templates cannot find the jquery
$even though in myapp.jsI have this imported. I have put a simple<script>tag in theform.html.eextemplate which will use the$jQuery selector, and I get an error saying$ is not defined- why is this? I thought $ was global when copied topriv/static/js/app.js?
Thanks
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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…
And in the corresponding view, I put
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…
By the way I use provide plugin in webpack config to load jquery.
PS. One quick test would be to move the loading of app.js before rendering template…
phoebe
Thanks, it works.
Now where do I put the
page_tree.jsscript in my webpack config (I would prefer this over an import inapp.js)? Does each page’s own js script have to be configured in webpack, it would seem a bit long winded to do so each time?kokolegorille
I put this file under /assets/static/js/
This way it will copied automatically to priv… (by CopyWebpackPlugin)
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.
phoebe
Thanks, actually I might have been confused earlier:
$is still undefined in thepage_tree.jscode.I can access
$in the the console window of developer tools, but is undefined inpage_tree.jswhere I tried to do run a simple$.ready( { .. });I removed
import $ from "jquery"inapp.jsafter making it global via webpack as you said:and the script should be getting rendered after
app.js. Inapp.htm.eex:and the corresponding view has been set up as per your suggestion.
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 onwindow.$instead.ion
Thank you, this worked. I also didn’t need to use ProvidePlugin.
Kurisu
Well I had the same problem and finally got an solution here jQuery Plugins and Legacy Applications.
Just move directly to the last section of the page
That’s it ! ^^