English 中文(简体)
How to update assigns inside phx-update="stream" block of code in LiveView
原标题:

I have a list of notes to show using LiveView stream, and I want to use an assign called online_users_map continuously updated by Phoenix Presence, in order to show which note s author is currently online . In the sample code below, I noticed the first inspect prints the updated map as users join or leave, but the second inspect remains %{} which leads me to think assigns in a stream phx-update block of code is static. Is that correct? If so, how do I make the dynamically changing assign value available in the stream block?

   <div>
    <%= inspect(@online_users_map) %>   <<< This prints correctly
      <ul
        phx-update="stream"
        phx-viewport-top={@page_no > 1 && "prev-page"}
        phx-viewport-bottom={!@end_of_timeline? && "next-page"}
        phx-page-loading
        id="searched_notess"
        class={[
          if(@end_of_timeline?, do: "pb-4", else: "pb-[calc(200vh)]"),
          if(@page_no == 1, do: "pt-4", else: "pt-[calc(200vh)]")
        ]}
      >

      <%= inspect(@online_users_map) %>  <<< This only prints %{}

        <li
          :for={{dom_id, note} <- @streams.searched_notes}
          class="pb-2 sm:pb-2 mt-2 flex flex-row "
          id={dom_id}
        >
问题回答

暂无回答




相关问题
OutOfMemoryException on MemoryStream writing

I have a little sample application I was working on trying to get some of the new .Net 4.0 Parallel Extensions going (they are very nice). I m running into a (probably really stupid) problem with an ...

flush in java.io.FileWriter

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush?? EDIT: So flush what it ...

Creating a FILE * stream that results in a string

I m looking for a way to pass in a FILE * to some function so that the function can write to it with fprintf. This is easy if I want the output to turn up in an actual file on disk, say. But what I ...

Most performant way to write binary data to a file in C#

I am trying to optimize a class that serializes objects in binary format and writes them in a file. I am currently using a FileStream (in sync mode because of the size of my objects) and a ...

热门标签