Slide 32
Slide 32 text
const { rubyVm } = await DefaultRubyVM(await WebAssembly.compileStreaming(res));
rubyVm.eval(`
require "js"
doc = JS.global[:document]
nodes = doc.call(:querySelectorAll, ".ikura_point")
nodes[:length].to_i.times do |i|
node.call(:addEventListener, "click", ->(event) {
…
JS.global.fetch("/ikura", opts).call(:then, ->(response) {
response.text().call(:then, ->(text) {
doc = JS.global[:document]
div = doc.call(:createElement, "div")
div[:innerHTML] = text.to_s
div.call(:querySelectorAll, "turbo-stream")[:length].to_i.times do |j|
el = div.call(:querySelectorAll, "turbo-stream").call(:item, j)
action = el.call(:getAttribute, "action").to_s
target = el.call(:getAttribute, "target").to_s
content = el.call(:querySelector, "template")[:innerHTML].to_s rescue ""
target_node = doc.call(:getElementById, target)
case action
when "append" then target_node.call(:insertAdjacentHTML, "beforeend", content)
end
end
Parse the Turbo Stream response received from the server and
append the ikura HTML to the gunkan-maki.
サーバーから受け取ったTurbo Streamsレスポンスを解析し、
いくらのHTMLを軍艦巻きの上にappendできるよう処理を追加