peter-de-boer

peter-de-boer

Javascript hook expects handle_event in all live views

I have implemented a Javascript hook for calculating the screen width and height as I need that info in one of my live views. I have basically used some code from this topic: https://forum.elixirforum.com/t/liveview-sending-more-data-than-expected/36259. So I have an event listener which pushes an event whenever the window is resized. This is handled by an appropriate handle_event in the liveivew that needs it.

The problem is that my application contains more than one liveview. Now when I navigate to a different live view, I get an error message like ** (FunctionClauseError) no function clause matching in BwWeb.StoreLive.handle_event/3.

So, am I supposed to implement an handle_event for this javascript event in every live view in my application, i.e. also in the ones that do not need that info? Or is there a more clever way to solve this?

Most Liked

mcrumm

mcrumm

Phoenix Core Team

You can use server lifecycle hooks to attach a shared handle_event on each LiveView. You can use the on_mount macro or the :on_mount option on the live_session router macro to reduce even more duplication:

#router.ex

live_session :default, on_mount: [MyAppWeb.SharedEvents] do
  # routes...
end

# shared_events.ex

defmodule MyAppWeb.SharedEvents to
  @moduledoc "Event handlers shared on all LiveView modules"
  use Phoenix.Component

  def on_mount(:default, _params, _session, socket) do
    socket = attach_hook(socket, :resize, :handle_event, &handle_event/3)
    {:cont, socket}
  end

  def handle_event("resize", _params, socket) do
    # handle the resize
    {:cont, socket}
  end
end
alexl

alexl

I’ve the same error, do you resolved it ?

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
LegitStack
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
vonH
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 Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
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
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New

We're in Beta

About us Mission Statement