Loading...
Loading...
Compare original and translation side by side
NO DATABASE QUERIES IN MOUNTdef mount(_params, _session, socket) do
# NO database queries here! Called twice.
{:ok, assign(socket, posts: [], loading: true)}
end
def handle_params(params, _uri, socket) do
# Database queries here - once per navigation
posts = Blog.list_posts(socket.assigns.scope)
{:noreply, assign(socket, posts: posts, loading: false)}
endNO DATABASE QUERIES IN MOUNTdef mount(_params, _session, socket) do
# 此处禁止数据库查询!会被调用两次。
{:ok, assign(socket, posts: [], loading: true)}
end
def handle_params(params, _uri, socket) do
# 数据库查询放在这里——每次导航执行一次
posts = Blog.list_posts(socket.assigns.scope)
{:noreply, assign(socket, posts: posts, loading: false)}
enddef list_posts(%Scope{user: user}) do
Post |> where(user_id: ^user.id) |> Repo.all()
enddef list_posts(%Scope{user: user}) do
Post |> where(user_id: ^user.id) |> Repo.all()
enddef subscribe(%Scope{organization: org}) do
Phoenix.PubSub.subscribe(@pubsub, "posts:org:#{org.id}")
enddef subscribe(%Scope{organization: org}) do
Phoenix.PubSub.subscribe(@pubsub, "posts:org:#{org.id}")
endassign_async/3def mount(_params, _session, socket) do
{:ok, assign_async(socket, :user, fn -> {:ok, %{user: fetch_user()}} end)}
endassign_async/3def mount(_params, _session, socket) do
{:ok, assign_async(socket, :user, fn -> {:ok, %{user: fetch_user()}} end)}
endterminate/2Process.monitor/1:DOWNterminate/2Process.monitor/1:DOWNstart_asynccancel_async/3start_asynccancel_async/3handle_outhandle_outbtn-primarybg-red-500btn-primarybg-red-500:content_type%Plug.Upload{}%Plug.Upload{}:content_type{:ok, body, conn} = Plug.Conn.read_body(conn)
verify_signature!(conn, body)
%{conn | body_params: JSON.decode!(body)}preserve_req_body: true{:ok, body, conn} = Plug.Conn.read_body(conn)
verify_signature!(conn, body)
%{conn | body_params: JSON.decode!(body)}preserve_req_body: true%Plug.Upload{}.content_type%Plug.Upload{}.content_type