dojap

dojap

I am having troubles with running JavaScript inside Phoenix.

Things that work in a normal html file doesn’t seem to work from Phoenix LiveView templates.

I need some basic stuff to work and can’t find any tutorial or docs.

Thanks for any links.

Showing Posts 1 to 7

tomkonidas

tomkonidas

What you are looking for is Javascript Interop in the Phoenix LiveView docs.

dojap

dojap OP

I don’t understand anything there. Seems like Phoenix is too complicated for me ;(.

Isn’t there any real world example when you want to use JavaScript inside templates of Phalcon?

I don’t want to involve the server at all, all I want to have a simple button click check based on a cookie state. But the problem is that the cookie is for some reason ignored. Defer in script or moving script at the end isnt helping. In plain html it works as stated here: Cookies in Phoenix LiveView not taken into account only after the second F5 refresh

tomkonidas

tomkonidas

So what you would do is set up a hook in your JS that your button will trigger, and then in the Hook (JS) you could access your cookie, do whatever you want, and push the result back to the LiveView to be handled.

dojap

dojap OP

I want everything to execute in browser, don’t want to send back and forth code to the backend. the cookies are stored in the browser it should be done everything on the client locally. Check this code of mine: Cookies in Phoenix LiveView not taken into account only after the second F5 refresh - #3 by dojap

wanton7

wanton7

I’m not sure why you split this to multiple threads? You could have just written more to the original thread if you think you didn’t get all the answers you needed.

You need to use LiveSocket’s params to pass initial values. So you need to get values from cookies and pass them as params for LiveSocket. Then you need to use that passed parameter to show/hide the tool tip in LiveView’s view markup. There is already example in your other thread that passes _csrf_token as param.

I want everything to execute in browser, don’t want to send back and forth code to the backend

If you want everything to execute in browser are you sure LiveView is really what you want to use?

wanton7

wanton7

Also personally I would use localStorage, it will work great with LiveSocket params and I’ve done just that in one of my personal project. Both cookie and localStorage are shared between browser tabs so you can get into all kinds of oddities if you switch between browser tabs that contain you app/site. Better solution is to read the cookie/localStorage to a variable on page load. Then always use that variable but when you change it write value to both, that variable and cookie/localStorage.

To summarize:

  • During page load read “showTips” from localStorage/cookie to a local variable called showTips
  • Pass showTips as LiveSocket param
  • Use showTips param in your live view markup to show/hide tips
  • Use showTips variable when you check if tips should be shown or not in the browser
  • If showTips variable is changed also write same value to “showTips” in localStorage/cookie.
edisonywh

edisonywh

That is not how Phoenix LiveView work, there’s a couple of great resources out there explaining the programming model (Chris has a great video on it, though I can’t remember which conference it was).

The rough outline is that, when a user visits a LiveView page, Phoenix sends down a bunch of HTML down the server, the client then connects to LiveView via WebSocket (powered by Phoenix Channels). Then, given that you have attributes like phx-click, these events are then sent to LiveView via websocket, and LiveView responds via websocket - as you can see, nothing is done “fully on browser” as you initially thought.

This is where the JS interops come in though, for example say things like a menu state where you don’t need to send things to backend, you can do it fully locally like you want with JS interops. There’s two ways to do this, either via LiveView’s hooks, or via external dependency like Alpine.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews