Skip to content

Commit

Permalink
WIP tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp committed Feb 18, 2011
1 parent 66aa320 commit 929074e
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 9 deletions.
2 changes: 1 addition & 1 deletion samples/shopwithme/project/build/LiftProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
val liftVersion = "2.3-SNAPSHOT"

// uncomment the following if you want to use the snapshot repo
// val scalatoolsSnapshot = ScalaToolsSnapshots
val scalatoolsSnapshot = ScalaToolsSnapshots

// The local maven repo to get immediate fixes
// val mavenLocal = "Local Maven Repository" at
Expand Down
42 changes: 34 additions & 8 deletions samples/shopwithme/src/main/scala/code/comet/CometCart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package comet
import lib._

import net.liftweb._
import common._
import http._
import util._
import js._
Expand All @@ -12,32 +13,53 @@ import JsCmds._
import scala.xml.NodeSeq
import Helpers._

/**
* What's the current cart for this session
*/
object TheCart extends SessionVar(new Cart())

/**
* The current instance of the CometCart
*/
object CometCartInstance extends SessionVar[Box[CometCart]](Empty)

/**
* The CometCart is the CometActor the represents the shopping cart
*/
class CometCart extends CometActor {
// our current cart
private var cart = TheCart.get

def render = Noop

override def fixedRender = {
/**
* Draw yourself
*/
def render = {
("#contents" #> (
"tbody" #>
Helpers.findOrCreateId(id =>
Helpers.findOrCreateId(id => // make sure tbody has an id
// when the cart contents updates
WiringUI.history(cart.contents) {
(old, nw, ns) => {
// capture the tr part of the template
val theTR = ("tr ^^" #> "**")(ns)


// build a row out of a cart item
def html(ci: CartItem): NodeSeq =
("tr [id]" #> ci.id & "td *" #> ci.name)(theTR)

// calculate the delta between the lists and
// based on the deltas, emit the current jQuery
// stuff to update the display
JqWiringSupport.calculateDeltas(old, nw, id)(_.id, html _)
}
})) &
"#total" #> WiringUI.asText(cart.subtotal))
"#total" #> WiringUI.asText(cart.subtotal)) // display the total
}


/**
* Process messages from external sources
*/
override def lowPriority = {

// if someone sends up a new cart
case SetNewCart(newCart) => {
// unregister from the old cart
Expand All @@ -51,6 +73,10 @@ class CometCart extends CometActor {
reRender(true)
}
}

override def localSetup() {
println("Me "+this+" name "+name)
}
}

/**
Expand Down
44 changes: 44 additions & 0 deletions samples/shopwithme/src/main/scala/code/comet/ListPages.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package code
package comet

import lib._

import net.liftweb._
import common._
import http._
import util._
import js._
import js.jquery._
import JsCmds._
import scala.xml.NodeSeq
import Helpers._

/**
* The list of pages being viewed in this session
*/
class ListPages extends CometActor {
/**
* Draw yourself
*/
def render = {
"#listPagesContents" #> (
"tbody" #>
Helpers.findOrCreateId(id => // make sure tbody has an id
// when the cart contents updates
WiringUI.history(SessionPresenceInfo.pages) {
(old, nw, ns) => {
// capture the tr part of the template
val theTR = ("tr ^^" #> "**")(ns)

// build a row out of the page view info
def html(ci: (String, ParsePath)): NodeSeq =
("tr [id]" #> ci._1 & "td *" #> ci._2.toString)(theTR)

// calculate the delta between the lists and
// based on the deltas, emit the current jQuery
// stuff to update the display
JqWiringSupport.calculateDeltas(old, nw, id)(_._1, html _)
}
}))
}
}
76 changes: 76 additions & 0 deletions samples/shopwithme/src/main/scala/code/comet/WhatPage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package code
package comet

import lib._

import net.liftweb._
import common._
import http._
import util._
import Helpers._
import scala.xml.NodeSeq

/**
* A simple presence CometActor
*/
class WhatPage extends CometActor {
private var thePath: Box[ParsePath] = Empty

override def lifespan = Full(3 seconds)

def render = NodeSeq.Empty

/**
* Is this CometActor going to capture the initial Req
* object? If yes, override this method and return true
* and override captureInitialReq to capture the Req. Why
* have to explicitly ask for the Req? In order to send Req
* instances across threads, the Req objects must be snapshotted
* which is the process of reading the POST or PUT body from the
* HTTP request stream. We don't want to do this unless we
* have to, so by default the Req is not snapshotted/sent. But
* if you want it, you can have it.
*/
override def sendInitialReq_? : Boolean = true

/**
* Comet Actors live outside the HTTP request/response cycle.
* However, it may be useful to know what Request led to the
* creation of the CometActor. You can override this method
* and capture the initial Req object. Note that keeping a reference
* to the Req may lead to memory retention issues if the Req contains
* large message bodies, etc. It's optimal to capture the path
* or capture any request parameters that you care about rather
* the keeping the whole Req reference.
*/
override protected def captureInitialReq(initialReq: Box[Req]) {
thePath = initialReq.map(_.path)

// add the parse path to the list of pages being viewed
// by this session
thePath.foreach(p => SessionPresenceInfo.pages.
atomicUpdate(lst => (uniqueId -> p) :: lst))

println("Initied "+name+" path "+thePath)
}

override def localShutdown() {
// remove the page from the list of pages
SessionPresenceInfo.pages.atomicUpdate(_.filter(_._1 != uniqueId))
println("Bye "+name+" "+SessionPresenceInfo.pages.get)
}

override def localSetup() {
println("Setting up in "+this+" 888888888888* ")
}
}

/**
* Capture the list of all the pages that
* are being viewed
*/
class PresenceInfo {
val pages = ValueCell[List[(String, ParsePath)]](Nil)
}

object SessionPresenceInfo extends SessionVar(new PresenceInfo)
13 changes: 13 additions & 0 deletions samples/shopwithme/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
</head>
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">

<div>
I'm on pages:

<div lift="comet?type=ListPages">
<table id="listPagesContents">
<tbody>
<tr><td>an item</td></tr>
</tbody>
</table>
</div>
</div>

<div lift="comet?type=CometCart">
Contents:
<table id="contents">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ <h1 class="alt">app<img alt="" id="ajax-loader" style="display:none; margin-bott

<div class="lift:Msgs?showAll=true"></div>
<hr class="space" />

<div class="lift:comet?type=WhatPage;randomname=true"></div>
</div>

<div class="column span-17 last">
Expand Down

0 comments on commit 929074e

Please sign in to comment.