Skip to content

Commit 413f006

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 d19f188 commit 413f006

File tree

2 files changed

+18
-51
lines changed

2 files changed

+18
-51
lines changed

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

+14-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
@@ -118,21 +119,23 @@ object EventHandler{
118119
}
119120
}
120121

121-
@JSExportTopLevel("ExampleXMLHttpRequest")
122-
object XMLHttpRequest{
122+
@JSExportTopLevel("ExampleFetch")
123+
object Fetch {
123124
@JSExport
124125
def main(pre: html.Pre) = {
125-
val xhr = new dom.XMLHttpRequest()
126-
xhr.open("GET",
126+
import scala.concurrent
127+
.ExecutionContext
128+
.Implicits
129+
.global
130+
import js.Thenable.Implicits._
131+
val url =
127132
"https://www.boredapi.com/api/activity"
128-
)
129-
xhr.onload = { (e: dom.Event) =>
130-
if (xhr.status == 200) {
131-
pre.textContent =
132-
xhr.responseText
133-
}
133+
for {
134+
response <- dom.fetch(url)
135+
text <- response.text()
136+
} {
137+
pre.textContent = text
134138
}
135-
xhr.send()
136139
}
137140
}
138141

@@ -155,20 +158,3 @@ object Websocket {
155158
}
156159
}
157160
}
158-
159-
@JSExportTopLevel("ExampleAjaxExtension")
160-
object AjaxExtension {
161-
@JSExport
162-
def main(pre: html.Pre) = {
163-
import dom.ext.Ajax
164-
import scala.concurrent
165-
.ExecutionContext
166-
.Implicits
167-
.global
168-
val url =
169-
"https://www.boredapi.com/api/activity"
170-
Ajax.get(url).foreach { case xhr =>
171-
pre.textContent = xhr.responseText
172-
}
173-
}
174-
}

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)