Skip to content

Commit c53e928

Browse files
committed
Use fetch in the readme.
Instead of XMLHttpRequest and the Ajax extension. Update the readme to remove references to the "extensions" in the process.
1 parent b339115 commit c53e928

File tree

2 files changed

+20
-51
lines changed

2 files changed

+20
-51
lines changed

example/src/main/scala/example/Example.scala

+16-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package example
22

3+
import scala.scalajs.js
34
import scala.scalajs.js.annotation._
45

56
import org.scalajs.dom
@@ -125,21 +126,25 @@ object EventHandler{
125126
}
126127
}
127128

128-
@JSExportTopLevel("ExampleXMLHttpRequest")
129-
object XMLHttpRequest{
129+
@JSExportTopLevel("ExampleFetch")
130+
object Fetch {
130131
@JSExport
131132
def main(pre: html.Pre) = {
132-
val xhr = new dom.XMLHttpRequest()
133-
xhr.open("GET",
133+
import scala.concurrent
134+
.ExecutionContext
135+
.Implicits
136+
.global
137+
import js.Thenable.Implicits._
138+
val url =
134139
"https://www.boredapi.com/api/activity"
135-
)
136-
xhr.onload = { (e: dom.Event) =>
137-
if (xhr.status == 200) {
138-
pre.textContent =
139-
xhr.responseText
140-
}
140+
val responseText = for {
141+
response <- dom.fetch(url)
142+
text <- response.text()
143+
} yield {
144+
text
141145
}
142-
xhr.send()
146+
for (text <- responseText)
147+
pre.textContent = text
143148
}
144149
}
145150

@@ -162,20 +167,3 @@ object Websocket {
162167
}
163168
}
164169
}
165-
166-
@JSExportTopLevel("ExampleAjaxExtension")
167-
object AjaxExtension {
168-
@JSExport
169-
def main(pre: html.Pre) = {
170-
import dom.ext.Ajax
171-
import scala.concurrent
172-
.ExecutionContext
173-
.Implicits
174-
.global
175-
val url =
176-
"https://www.boredapi.com/api/activity"
177-
Ajax.get(url).foreach { case xhr =>
178-
pre.textContent = xhr.responseText
179-
}
180-
}
181-
}

readme/Index.scalatex

+4-23
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@
115115
autorun=true
116116
)
117117

118-
@sect{dom.XMLHttpRequest}
118+
@sect{dom.Fetch}
119119
@pair(
120-
"XMLHttpRequest",
120+
"Fetch",
121121
Seq(
122122
pre("output")
123123
)
@@ -145,38 +145,19 @@
145145
@li
146146
Deprecated properties/methods/types will not be present.
147147
@li
148-
IE-only, Chrome-only, FF-only, and in general browser-specific attributes will only be found under the @hl.scala{experimental} package.
148+
IE-only, Chrome-only, FF-only, and in general browser-specific attributes will typically not be present.
149149
@li
150150
The name of a Scala type should map directly to the name of the corresponding Javascript type.
151151
@li
152152
Any type which is a Javascript type (e.g. you can @hl.scala{instanceof} in javascript) should be a Scala @hl.scala{class}; any other interface which isn't a Javascript type should be a @hl.scala{trait}.
153153
@li
154154
Read-only members should be @hl.scala{def}, and not-directly-instantiable classes should have @hl.scala{private} constructors.
155155

156-
@sect{Extensions}
157-
158-
@p
159-
Apart from @hl.scala{Color}, Scala-js-dom contains some useful helpers and implicit classes in @hl.scala{org.scalajs.dom.ext} that serve no purpose other than to make your use of the DOM more pleasant.
160-
161-
@p
162-
Examples include the @hl.scala{Ajax.get} and @hl.scala{Ajax.post} methods which let you avoid messing with @hl.scala{dom.XMLHttpRequest} directly, or @hl.scala{KeyCodes} which provides a nice list of the keycodes that result from pressing various keys on the keyboard.
163-
@pair(
164-
"AjaxExtension",
165-
Seq(
166-
pre("output")
167-
)
168-
)
169-
170-
@p
171-
See also @a("roll", href:="https://github.com/lihaoyi/roll") (@a("live demo", href:="http://lihaoyi.github.io/roll/")) and @a("scala-js-games", href:="https://github.com/lihaoyi/scala-js-games") for an example of its use. @a("Scala-js-fiddle", href:="http://www.scala-js-fiddle.com/") also contains a pile of @a("fun examples", href:="(http://www.scala-js-fiddle.com/gist/9405209/Oscilloscope.scala") that demonstrate its usage. Pull requests/forks are welcome!
172-
173156
@sect{Contributing}
174157
@p
175-
Scala-js-dom is a work in progress. The current code base is a hodgepodge of auto-generated/scraped/hand-tweaked code, and is full of rough edges. If you see something that you think can be improved, feel free to send a pull request. These could include:
158+
The DOM API is always evolving, and scala-js-dom is a hodgepodge of auto-generated/scraped/hand-tweaked code full of rough edges. If you see something that you think can be improved, feel free to send a pull request. These could include:
176159
@ul
177160
@li
178161
Improved doc-comments; who doesn't love better docs?
179162
@li
180163
Missing methods/properties/classes; send the PR adding it in including it together with a link to an authoritative source (e.g. MDN) and it should get merged.
181-
@li
182-
Additional extensions (in @hl.scala{org.scalajs.dom.ext}). These currently represent an arbitrary collection of helpers that have been needed so far. If there's some implicit that you find you need and you think other people will to, send a pull request and we can talk about it.

0 commit comments

Comments
 (0)