mirror of
https://github.com/woutdp/live_svelte
synced 2026-05-24 09:28:21 +00:00
Use body instead of name in example
This commit is contained in:
parent
6be33b8c7c
commit
6be5be445c
2 changed files with 9 additions and 9 deletions
|
|
@ -3,14 +3,14 @@
|
|||
|
||||
export let pushEvent
|
||||
export let items = []
|
||||
let newItemName
|
||||
let body
|
||||
let i = 1
|
||||
let showItems = true
|
||||
|
||||
function addItem() {
|
||||
if (!newItemName) return
|
||||
pushEvent("add_item", {name: newItemName})
|
||||
newItemName = ""
|
||||
if (!body) return
|
||||
pushEvent("add_item", {body})
|
||||
body = ""
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
<div class="mb-2">
|
||||
<form>
|
||||
<input type="test" bind:value={newItemName} class="border rounded px-2 py-1" />
|
||||
<input type="test" bind:value={body} class="border rounded px-2 py-1" />
|
||||
<button type="submit" class="bg-black rounded text-white px-2 py-1 font-bold" on:click|preventDefault={addItem}>
|
||||
Add item
|
||||
</button>
|
||||
|
|
@ -40,7 +40,7 @@
|
|||
{#each items.slice(0, i) as item (item.id)}
|
||||
<div in:fly={{x: -40}}>
|
||||
<div transition:slide|local class="py-2 border-t border-[#eee]">
|
||||
{item.id}: {item.name}
|
||||
{item.id}: {item.body}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ defmodule ExampleWeb.LiveExample4 do
|
|||
{:ok, assign(socket, :items, [])}
|
||||
end
|
||||
|
||||
def handle_event("add_item", %{"name" => name}, socket) do
|
||||
{:noreply, assign(socket, :items, add_log(socket, name))}
|
||||
def handle_event("add_item", %{"body" => body}, socket) do
|
||||
{:noreply, assign(socket, :items, add_log(socket, body))}
|
||||
end
|
||||
|
||||
def handle_info(:tick, socket) do
|
||||
|
|
@ -25,6 +25,6 @@ defmodule ExampleWeb.LiveExample4 do
|
|||
end
|
||||
|
||||
defp add_log(socket, body) do
|
||||
[%{id: System.unique_integer([:positive]), name: body} | socket.assigns.items]
|
||||
[%{id: System.unique_integer([:positive]), body: body} | socket.assigns.items]
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue