scala - How to married scalajs-react and autowire on a client side? -
i have trouble mix autowire calls scalajs-react components. here simple example using 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 deal 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 $name") } .componentdidmount(_.backend.mounted) .buildu
state modification doesn't work here. how can combine future
, callback
?
update (27/10/15)
my autowire client
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) } }
adding methods make combination of callback
+future
easier might helpful. feel free submit pr, or raise issue showing examples additional support useful.
until such helpers exist, can call .runnow()
inside future this: callback(future.foreach($.setstate(_).runnow()))
Comments
Post a Comment