Joshua-at-bitcrowd
`on_mount` called from within `@before_compile` doesn't execute. Defect or on purpose?
Hey,
so I had this situation:
defmodule HelloWeb.Foo do
use HelloWeb, :live_view
use HelloWeb.Bar
on_mount(__MODULE__)
def on_mount(:default, _params, _session, socket) do
dbg("on_mount Foo")
{:cont, socket}
end
end
defmodule HelloWeb.Bar do
defmacro __using__(_) do
quote do
on_mount(HelloWeb.Bar)
end
end
def on_mount(:default, _params, _session, socket) do
dbg("on_mount Bar")
{:cont, socket}
end
end
Giving me the debug output:
[lib/hello_web/live/bar.ex:9: HelloWeb.Bar.on_mount/4]
"on_mount Bar" #=> "on_mount Bar"
[lib/hello_web/live/foo.ex:8: HelloWeb.Foo.on_mount/4]
"on_mount Foo" #=> "on_mount Foo"
But I would like the on_mount in Bar to be called last, so I modified it like this:
defmodule HelloWeb.Bar do
defmacro __using__(_) do
quote do
@before_compile(HelloWeb.Bar)
end
end
defmacro __before_compile__(_env) do
quote do
on_mount(HelloWeb.Bar)
end
end
def on_mount(:default, _params, _session, socket) do
dbg("on_mount Bar")
{:cont, socket}
end
end
With the debug output:
[(hello 0.1.0) lib/hello_web/live/foo.ex:8: HelloWeb.Foo.on_mount/4]
"on_mount Foo" #=> "on_mount Foo"
Raising my question:
Why is Bar.on_mount not called?
I’m aware that I could:
- switch places of
use HelloWeb.Barandon_mount(__MODULE__) - call
on_mount(HelloWeb.Bar)fromFooafteron_mount(__MODULE__) - use the
mountcallback inHelloWeb.Bar.__using__, which is called after all theon_mountcalls.
All have there own drawbacks.
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
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
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
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
I would like to know what is the best IDE for elixir development?
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









