Skip to content

Instantly share code, notes, and snippets.

@iketiunn
Created July 20, 2020 08:16
Show Gist options
  • Save iketiunn/dba96ca973816d1e4e929bcd69388a8d to your computer and use it in GitHub Desktop.
Save iketiunn/dba96ca973816d1e4e929bcd69388a8d to your computer and use it in GitHub Desktop.
liveview example
defmodule HanxinWeb.CounterLive do
use HanxinWeb, :live_view
def mount(_params, _session, socket) do
{:ok, assign(socket, hello: :world, count: 0)}
end
def render(assigns) do
~L"""
<H1>Hello <%= assigns.hello %></H1>
<p>
count: <%= assigns.count %>
<div>
<button phx-click="+">+</button>
<button phx-click="-">-</button>
</div>
</p>
"""
end
def handle_event("+", _, socket) do
{:noreply, assign(socket, count: socket.assigns.count + 1)}
end
def handle_event("-", _, socket) do
{:noreply, assign(socket, count: socket.assigns.count - 1)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment