Javascript 我的PlayFramework操作在未来准备就绪之前返回,如何更新网页组件?

Javascript 我的PlayFramework操作在未来准备就绪之前返回,如何更新网页组件?,javascript,mongodb,scala,future,Javascript,Mongodb,Scala,Future,我有一个Scala PlayFramework函数,它调用MongoDB并获得一个Future[Seq[Document]]结果。在地图缩放/平移事件之后,通过xhttp/GET从网页上的JavaScript调用PlayAction函数。在未来的onComplete/Success执行之前,游戏端的我的动作方法返回。因此,我正在寻找一种在Scala Future的onComplete/Success启动时调用JavaScript函数来获取数据的方法。我该怎么做,还是我看错了 这是有问题的代码 d

我有一个Scala PlayFramework函数,它调用MongoDB并获得一个
Future[Seq[Document]]
结果。在地图缩放/平移事件之后,通过xhttp/GET从网页上的JavaScript调用Play
Action
函数。在未来的
onComplete/Success
执行之前,游戏端的我的动作方法返回。因此,我正在寻找一种在Scala Future的onComplete/Success启动时调用JavaScript函数来获取数据的方法。我该怎么做,还是我看错了

这是有问题的代码

def rect(swLon: Float, swLat: Float, neLon: Float, neLat: Float) = Action {
  val sb = new StringBuilder()
  sb.append("<tt>boundingBox: swLon=" + swLon + ", swLat=" + swLat + ", neLon=" + neLon + ", neLat=" + neLat + "</tt>")
  if (oDb.isDefined) {
    val collection: MongoCollection[Document] = oDb.get.getCollection(collectionName)
    val fut = getFutureOne(collection) // returns a Future[Seq[Document]]
    fut onComplete {
      case Success(docs) => { for (doc <- docs) { setMongoJson(doc.toJson } }
      case Failure(t) => { println("FAIL: " + t.getMessage) }
    }
  }
  Ok(sb.toString)
}

// below is temporary until I figure out a better way to store/return the result when it comes in
private var mongoJson: String = ""
private def setMongoJson(s: String): Unit = mongoJson = s
def rect(swLon:Float,swLat:Float,neLon:Float,neLat:Float)=操作{
val sb=新的StringBuilder()
sb.追加(“边界框:swLon=“+swLon+”,swLat=“+swLat+”,neLon=“+neLon+”,neLat=“+neLat+”)
如果(已定义oDb){
val集合:MongoCollection[文档]=oDb.get.getCollection(collectionName)
val fut=getFutureOne(collection)//返回未来[Seq[Document]]
未完成{
案例成功(docs)=>{for(doc{println(“FAIL:+t.getMessage”)}
}
}
好的(某人扭动)
}
//下面是暂时的,直到我找到一个更好的方法来存储/返回结果
私有变量mongoJson:String=“”
私有def setMongoJson(s:String):Unit=mongoJson=s

getFutureOne
是临时的,它只执行一个
db.collection.find().first().toFuture
。我只是想确保我与MongoDB的连接正常工作。事实上,我将用一个查询来替换它,以返回在边界框内的数据。

操作
不是为期货设计的。使用
操作.async
,它将“等待”(技术上不是等待,而是计划)为了将来完成:

def rect(swLon: Float, swLat: Float, neLon: Float, neLat: Float) = Action.async {
  val sb = new StringBuilder()
  sb.append("<tt>boundingBox: swLon=" + swLon + ", swLat=" + swLat + ", neLon=" + neLon + ", neLat=" + neLat + "</tt>")
  if (oDb.isDefined) {
    val collection: MongoCollection[Document] = oDb.get.getCollection(collectionName)
    val fut = getFutureOne(collection) // returns a Future[Seq[Document]]
    fut.map {docs => 
      setMongoJson(doc.toJson)
      Ok(sb.toString)
    } recover {
      case e => BadRequest("FAIL: " + e.getMessage)
    }
  } else Future.successful(Ok("Not defined"))
}
def rect(swLon:Float,swLat:Float,neLon:Float,neLat:Float)=Action.async{
val sb=新的StringBuilder()
sb.追加(“边界框:swLon=“+swLon+”,swLat=“+swLat+”,neLon=“+neLon+”,neLat=“+neLat+”)
如果(已定义oDb){
val集合:MongoCollection[文档]=oDb.get.getCollection(collectionName)
val fut=getFutureOne(collection)//返回未来[Seq[Document]]
fut.map{docs=>
setMongoJson(doc.toJson)
好的(某人扭动)
}恢复{
案例e=>BadRequest(“失败:+e.getMessage)
}
}else Future.successful(正常(“未定义”))
}
请看以下内容以供参考:
操作
不适用于未来。使用
操作.async
,它将“等待”(技术上不是等待,而是安排)未来完成:

def rect(swLon: Float, swLat: Float, neLon: Float, neLat: Float) = Action.async {
  val sb = new StringBuilder()
  sb.append("<tt>boundingBox: swLon=" + swLon + ", swLat=" + swLat + ", neLon=" + neLon + ", neLat=" + neLat + "</tt>")
  if (oDb.isDefined) {
    val collection: MongoCollection[Document] = oDb.get.getCollection(collectionName)
    val fut = getFutureOne(collection) // returns a Future[Seq[Document]]
    fut.map {docs => 
      setMongoJson(doc.toJson)
      Ok(sb.toString)
    } recover {
      case e => BadRequest("FAIL: " + e.getMessage)
    }
  } else Future.successful(Ok("Not defined"))
}
def rect(swLon:Float,swLat:Float,neLon:Float,neLat:Float)=Action.async{
val sb=新的StringBuilder()
sb.追加(“边界框:swLon=“+swLon+”,swLat=“+swLat+”,neLon=“+neLon+”,neLat=“+neLat+”)
如果(已定义oDb){
val集合:MongoCollection[文档]=oDb.get.getCollection(collectionName)
val fut=getFutureOne(collection)//返回未来[Seq[Document]]
fut.map{docs=>
setMongoJson(doc.toJson)
好的(某人扭动)
}恢复{
案例e=>BadRequest(“失败:+e.getMessage)
}
}else Future.successful(正常(“未定义”))
}
请看以下内容以供参考:

你能发布控制器操作的(可能简化的)代码吗?@Mikesname我编辑了这个问题,以包含相关的Scala代码。这并不漂亮,但首先我必须弄清楚如何实现这一点。你能发布(可能简化的)代码吗你的控制器操作代码?@Mikesname我编辑了这个问题,加入了相关的Scala代码。这并不漂亮,但首先我必须弄清楚如何使它工作。谢谢@Mikesname和Alvaro,我已经对代码进行了更改,我要看看它是否工作……谢谢@Mikesname和Alvaro,我已经对代码进行了更改,我要看看它是否工作Works。。。