Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Phoenix LiveView, finally

Phoenix LiveView, finally

Comment est-ce que Phoenix.LiveView nous amène à créer des comportements riches dans nos applications web sans architecture frontend complexe. Où et comment appliquer les concepts pour bénéficier des forces de BEAM dans votre navigateur.

Simon Prévost

May 08, 2019
Tweet

More Decks by Simon Prévost

Other Decks in Technology

Transcript

  1. Phoenix LiveView, finally MIREGO ACADEMY Objectifs Comprendre pourquoi et comment

    Phoenix LiveView révolutionne le développement d’application server-side riche. Structure Phoenix EEx LiveView Demo 1 2 3 4 Résumé . TITRE Phoenix LiveView, finally TYPE Google slide presentation DURÉE 20 min PRÉSENTATION DONNÉE PAR Simon Prévost AUDIENCE CIBLE Développeur web
  2. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    • Internet stuff • Cowboy stuff • Endpoint • Router • Controller • View • Templates Phoenix
  3. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    Endpoint plug(:canonical_host) plug(:force_ssl) plug(:basic_auth) plug(:session)
  4. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    • Internet stuff • Cowboy stuff • Endpoint • Router • Controller • View • Templates Phoenix
  5. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . View

    defmodule AppWeb.MyController do def index(conn, params) do conn |> put_view(AppWeb.MyView) |> render("index.json", params) end end defmodule AppWeb.MyView do def render("index.json", %{a: a, b: b}) do %{result: a + b} end end
  6. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . EEx

    (Embedded Elixir) # template.eex <%= a + b %> # sample.ex defmodule Sample do require EEx EEx.function_from_file(:def, :foo, "template.eex", [:a, :b]) end # iex Sample.foo(1, 2) #=> "3"
  7. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    Templates _ = var!(assigns) {:safe, [( tmp1 = ["" | "<h1>Listing Users</h1>\n\n<ul>\n "] [tmp1 | case(for(user <- Phoenix.HTML.Engine.fetch_assign(var!(assigns), :users)) do {:safe, [( tmp1 = [( tmp1 = ["" | "\n <li> "] [tmp1 | case(user.first_name()) do
  8. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    LiveView Templates # template.leex foo <%= @direction_1 %> bar <%= @direction_2 %> baz %Phoenix.LiveView.Rendered{ static: ["foo", "bar", "baz"], dynamic: ["left", "right"] } # ["foo ", "left", " bar ", "right", " baz"]
  9. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    LiveView Concept defmodule AppWeb.Live.MyComponent do use Phoenix.LiveView end
  10. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    LiveView Concept • use GenServer • Declarative events on server-rendered HTML • Messages from client or server • Server-side diff tracking
  11. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    LiveView Concept # app.js import LiveSocket from "phoenix_live_view" let liveSocket = new LiveSocket("/live") liveSocket.connect() # endpoint socket "/live", Phoenix.LiveView.Socket
  12. Phoenix LiveView, finally MIREGO ACADEMY Phoenix LiveView, finally . Phoenix

    LiveView Life-cycle use Phoenix.LiveView def render(assigns) do ~L""" <button phx-click="inc_temperature"> + <%= @count %> </button> """ end def handle_event(:inc_temperature, _value, socket) do new_count = socket.assigns[:count] + 1 {:noreply, assign(socket, :count, new_count) end