dillonoconnor

dillonoconnor

Navigation to LiveView from init

Is it possible to navigate to a LiveView in init (i.e. without clicking a link)? I am hoping to preserve my ash_authentication_phoenix sign_in LiveView without recreating it with Hologram.

Marked As Solved

bartblast

bartblast

Creator of Hologram

Not yet, but this will be possible with the planned middleware layer that’s coming to Hologram.

In the meantime, if you need this functionality now, there’s a workaround… though I should warn you it’s the kind of hack that makes you want to take a shower afterwards :see_no_evil_monkey:

The idea is:

  1. In your init/3, set some flag in the state (e.g., redirect_to_live_view: true)
  2. Use that flag in your layout template to either hide the page content or show a loading indicator
  3. Add put_action(:execute_js_redirect, ...) in your page init/3 function
  4. Implement that action to call Hologram.JS.exec/1 with your JavaScript redirect code

Something like:

def action(:execute_js_redirect, _params, component) do
  Hologram.JS.exec("window.location.href = '/your-liveview-path'")
  component
end

It’s not pretty, but it should let you preserve your ash_authentication_phoenix auth behaviour for now until proper support lands.

Hope this helps!

Ugh!..

Also Liked

dillonoconnor

dillonoconnor

The Hologram no-JS promise broken! T_T

Just kidding. This is really not that bad as a temporary fix! I appreciate the help–I was really much more concerned that I would have to figure out the auth page setup…

Looking forward to the future middleware :slight_smile: Thanks.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
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

We're in Beta

About us Mission Statement