Created
December 25, 2013 10:08
-
-
Save bblfish/8121905 to your computer and use it in GitHub Desktop.
A simple ajax call using scala-js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import scala.language.dynamics | |
import scala.scalajs.js | |
import js.Dynamic.{ global => g } | |
import org.scalajs.jquery.{jQuery => jQ, JQueryXHR, JQueryAjaxSettings} | |
//import scala.scalajs.js.{Dictionary, String, Any} | |
//code by Benjamin Jackman published to the Scala.js mailing list on Dec 16 | |
class JsObjectBuilder extends scala.Dynamic { | |
def applyDynamicNamed[A](name: String)(args: (String, js.Any)*): A = { | |
println(s"applyDynamicNamed($name)(args: $args") | |
if (name != "apply") { | |
sys.error("Call jsObj like this jsObj(x=1, y=2) which returns a javascript object that is {x:1,y:2}") | |
} | |
val obj = js.Object().asInstanceOf[js.Dictionary] | |
args.foreach { case (name, value) => | |
obj(name) = value | |
} | |
obj.asInstanceOf[A] | |
} | |
//Allows for jsObj() | |
def applyDynamic[A](name : String)(args: Nothing*) = { | |
if (args.nonEmpty) { | |
sys.error("Call jsObj only with named parameters.") | |
} | |
js.Object().asInstanceOf[A] | |
} | |
//Note that jsObj can also be given a type parameter | |
//that type will be used as the return type, | |
//However it's just a NOP and doesn't do real type | |
//safety | |
} | |
object ScalaJSExample { | |
val console = js.Dynamic.global.console | |
def main(): Unit = { | |
console.log("hello") | |
val title = jQ("#h1") | |
title.replaceWith("<h2>New Title<h2>") | |
val paragraph = g.document.createElement("p") | |
paragraph.innerHTML = "<strong>It works now!</strong>" | |
g.document.getElementById("playground").appendChild(paragraph) | |
val ttl=jQ("#turtle") | |
val jsObj = new JsObjectBuilder | |
val json = jsObj(url = "http://www.w3.org/People/Berners-Lee/card", | |
success = (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>{ | |
console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR"); | |
js.Dictionary() | |
}, | |
error = ( jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) => { | |
console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow"); | |
js.Dictionary() | |
}, | |
`type` = "GET" | |
) | |
jQ.ajax(json) | |
// jQ.ajax(js.Dictionary( | |
// "url" -> {val str: js.Any = "http://www.w3.org/People/Berners-Lee/card"; str}, | |
// "success" -> { val res: js.Any = test _; res }, | |
// "error" -> { val res: js.Any = err _; res } , | |
// "type" -> { val str: js.Any = "GET"; str } | |
// ).asInstanceOf[JQueryAjaxSettings] | |
// ) | |
} | |
private def test(data: js.Any, textStatus: js.String, jqXHR: JQueryXHR): js.Dynamic = { | |
console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR"); | |
null | |
} | |
private def err(jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) : js.Any = { | |
console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow"); | |
js.Dictionary() | |
} | |
} | |
found the answer: I forgot to type the answer jsObj[org.scalajs.jquery.JQueryAjaxSettings
. This compiles
and runs correctly:
val json = jsObj[org.scalajs.jquery.JQueryAjaxSettings](url = "http://www.w3.org/People/Berners-Lee/card",
success = (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>{
console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR");
js.Dictionary()
},
error = ( jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) => {
console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow");
js.Dictionary()
},
`type` = "GET"
)
jQ.ajax(json)
I'm building a cordova application using scalajs and these examples have been very useful ! Thanks.
But I can't get the scala named parameters to work... The compiler complains "Cannot resolve symbol url, success, error and type
"
Any idea of what could be wrong ?
Sebastien gave a new response to this here: https://groups.google.com/d/msg/scala-js/im08n5nxVnI/d31ibVSI_iUJ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above compiles but it gives me a
It does go into the applyDynamicNamed part and write out to the JS Console
The code generated by compiler "2.11.0-M7" which is close to the 2.10 one ( which has the same problem) is: