Skip to content

Commit 41ffd72

Browse files
HParkerkateinoigakukun
authored andcommitted
Update js.rb doc comment
This change does several things, - This fixes the typo in addEventListener - Updates the document to call addEventListener on a class that defines it - Adds an additional example for when using without stdlib
1 parent 802ecc5 commit 41ffd72

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ext/js/lib/js.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,31 @@
77
# require 'js'
88
# JS.eval("return 1 + 2") # => 3
99
# JS.global[:document].write("Hello, world!")
10-
# JS.global[:document].addEventListner("click") do |event|
10+
# div = JS.global[:document].createElement("div")
11+
# div[:innerText] = "click me"
12+
# JS.global[:document][:body].appendChild(div)
13+
# div.addEventListener("click") do |event|
1114
# puts event # => # [object MouseEvent]
1215
# puts event[:detail] # => 1
16+
# div[:innerText] = "clicked!"
17+
# end
18+
#
19+
# If you are using `ruby.wasm` without `stdlib` you will not have `addEventListener`
20+
# and other specialized functions defined. You can still acomplish many
21+
# of the same things using `call` instead.
22+
#
23+
# == Example
24+
#
25+
# require 'js'
26+
# JS.eval("return 1 + 2") # => 3
27+
# JS.global[:document].call(:write, "Hello, world!")
28+
# div = JS.global[:document].call(:createElement, "div")
29+
# div[:innerText] = "click me"
30+
# JS.global[:document][:body].call(:appendChild, div)
31+
# div.call(:addEventListener, "click") do |event|
32+
# puts event # => # [object MouseEvent]
33+
# puts event[:detail] # => 1
34+
# div[:innerText] = "clicked!"
1335
# end
1436
#
1537
module JS

0 commit comments

Comments
 (0)