我无法将 autowire 调用与 scalajs-react 组件混合使用。scalajs-react v0.10.0
这是一个使用+的简单示例autowire v0.2.5
case class User(name: String)
class Backend($: BackendScope[Unit, User]) {
def mounted: Callback = {
val future: Future[User] = AjaxClient[Api].getUser(123).call()
//FIXME: how to get deal with it?
CallbackTo { future.foreach(user => $.modState(_ => user)) }
}
}
val Main = ReactComponentB[Unit]("Main")
.initialState(User("N/A"))
.backend(new Backend(_))
.render { $ =>
val name = $.state.name
<.p(s"User name is $name")
}
.componentDidMount(_.backend.mounted)
.buildU
状态修改在这里不起作用。我怎样才能结合Future
和Callback
?
更新(27/10/15)
我的自动接线客户端
package example
import scalajs.concurrent.JSExecutionContext.Implicits.runNow
import org.scalajs.dom
import upickle._
import default._
import scala.concurrent.Future
object AjaxClient extends autowire.Client[Js.Value, Reader, Writer] {
def write[Result: Writer](r: Result) = default.writeJs(r)
def read[Result: Reader](p: Js.Value) = default.readJs[Result](p)
override def doCall(req: Request): Future[Js.Value] = {
println(req)
dom.ext.Ajax.post(
url = "/api/autowire/" + req.path.mkString("/"),
headers = Map("Content-Type" -> "application/json"),
data = json.write(Js.Obj(req.args.toSeq :_*))
).map(_.responseText)
.map{t => println(t); t}
.map(json.read)
}
}