Like a spreadsheet, Lift’s Wiring is based on Cells. Cells come in three types: ValueCell, DynamicCell, and FuncCell.
A ValueCell contains a value that is entered by a user or depends on some user action. A ValueCell may represent the items in our shopping cart or the tax rate.
A DynamicCell contains a value that changes every time the cell is accessed. For example, a random number or the current time.
A FuncCell has a value based on a formula applied to the value or other cells.
Let’s see some code that demonstrates this:
val quantity = ValueCell(0)
val price = ValueCell(1d)
val total = price.lift(_ * quantity)
We define two ValueCells, one for quantity and the other for price. Next, define the total by “lifting” the price in a formula that multiplies it by quantity. Let’s see how it works in the console:
scala> import net.liftweb._
import net.liftweb._
scala> import util._
import util._
scala> val quantity = ValueCell(0)
quantity: net.liftweb.util.ValueCell[Int] = ValueCell(0)
scala> val price = ValueCell(0d)
price: net.liftweb.util.ValueCell[Double] = ValueCell(0.0)
scala> val total = price.lift(_ * quantity)
total: net.liftweb.util.Cell[Double] = FuncCell1(ValueCell(0.0),<function1>)
scala> total.get
res1: Double = 0.0
scala> quantity.set(10)
res2: Int = 10
scala> price.set(0.5d)
res3: Double = 0.5
scala> total.get
res4: Double = 5.0
Okay... pretty nifty... we can define relationships that are arbitrarily complex between Cells and they know how to calculate themselves.
(C) 2012 David Pollak