Elixir / Phoenix: Views for channels? -


i'm creating chat app , have bunch of channel messages. here's 1 of them:

def handle_in("read", %{ "chat_id" => chat_id }, socket)   user_id = socket.assigns[:id]    ts = datetime.utc_now   case chatmanager.mark_as_read({user_id, chat_id, ts})     {:ok, chat_user} ->       last_read_at_unix = chat_user.last_read_at |> timeconverter.ecto_to_unix       {:reply, {:ok, %{ chat_id: chat_id, last_read_at: last_read_at_unix }}, socket}     {:error, changeset} ->       {:reply, {:error, %{errors: changeset.errors}}, socket}   end end 

can use phoenix views separate presentation / response logic? way can go view file , see returned each message.

phoenix views normal modules functions in them.

you can either call functions directly:

myapp.web.chatview.render("message.json", %{message: my_message}) 

or use phoenix.view function call render/2 function of view:

phoenix.view.render_one(myapp.web.chatview, "message.json", message: my_message) 

the phoenix.view functions have few advantages if arguments more dynamic (for example if pass nil message).

consult phoenix.view documentation details on convenience functions.

when building large application, makes sense have .json templates models because you'll need pass json around in api responses, channel messages or serialized messages in message queue. views you've written reusable of cases.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -