From a3d660bd2d501966949a4c04a6450050db6766f1 Mon Sep 17 00:00:00 2001 From: Joe Barnes Date: Sun, 27 Jul 2014 11:43:09 -0500 Subject: [PATCH] Corrected resolution for #1 by ensuring that messages get dequeued. Previously would only dequeue once a new message arrived. --- .../net/liftmodules/ng/AngularActor.scala | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/scala/net/liftmodules/ng/AngularActor.scala b/src/main/scala/net/liftmodules/ng/AngularActor.scala index 807a5c62..00e57cd1 100644 --- a/src/main/scala/net/liftmodules/ng/AngularActor.scala +++ b/src/main/scala/net/liftmodules/ng/AngularActor.scala @@ -10,6 +10,7 @@ import JsCmds._ import StringHelpers._ import json.Serialization._ import json.DefaultFormats +import json.JsonAST._ /** A comet actor for Angular action */ trait AngularActor extends CometActor with Loggable { @@ -35,18 +36,19 @@ trait AngularActor extends CometActor with Loggable { def assign(field:String, obj:AnyRef):Unit = partialUpdate(assignCmd(field, obj)) /** Variables needed to perform any of our angular actions (will be \$scope and possibly \$rootScope) */ - protected def vars:String + protected def vars:JsCmd /** The variable name of the scope variable for this scope (either \$scope or \$rootScope) */ protected def scopeVar:String - /** Variable assignment for \$scope */ - protected val varScope = "var s=angular.element(document.querySelector('#"+id+"')).scope();" - /** Variable assignment for \$rootScope */ - protected val varRoot = "var r=(typeof s==='undefined')?void 0:s.$root;" - /** Interval between tries to unload our early-arrival event queue */ private val interval = Props.getInt("net.liftmodules.ng.AngularActor.retryInterval", 100) + protected val varElement = JsCrVar("e", Call("angular.element", Call("document.querySelector", JString("#"+id))))// "var s=angular.element(document.querySelector('#"+id+"')).scope();" + /** Variable assignment for \$scope */ + protected val varScope = JsCrVar("s", AnonFunc(JsReturn(Call("e.scope")))) + /** Variable assignment for \$rootScope */ + protected val varRoot = JsCrVar("r", AnonFunc(JsReturn(JsRaw("(typeof s()==='undefined')?void 0:s().$root"))))// "var r=(typeof s==='undefined')?void 0:s.$root;" + /** Sends an event command, i.e. broadcast or emit */ private def eventCmd(method:String, event:String, obj:AnyRef):JsCmd = { doCmd(scopeVar+".$apply(function(){"+scopeVar+".$"+method+"('"+event+"',"+stringify(obj)+");});") @@ -73,13 +75,13 @@ trait AngularActor extends CometActor with Loggable { "};" val enqueue = "if(typeof net_liftmodules_ng_q==='undefined'){net_liftmodules_ng_q=[];setTimeout(function(){d();},"+interval+");}" + "net_liftmodules_ng_q.push({t:t,f:f});" - JsRaw(vars+ready+fn+dequeue+"if(typeof net_liftmodules_ng_q==='undefined'&&t()){f();}else{"+enqueue+"}") + vars & JsRaw(ready+fn+dequeue+"if(typeof net_liftmodules_ng_q==='undefined'&&t()){f();}else{"+enqueue+"}") } } private class ChildScope extends Scope { - override val vars = varScope - override val scopeVar = "s" + override val vars = varElement&varScope + override val scopeVar = "s()" } /** Your handle to the \$scope object for your actor */ @@ -87,8 +89,8 @@ trait AngularActor extends CometActor with Loggable { /** Your handle to the \$rootScope object for your actor */ object rootScope extends Scope { - override val vars = varScope+varRoot - override val scopeVar = "r" + override val vars = varElement&varScope&varRoot + override val scopeVar = "r()" } }