An example of using Lift's Wiring feature to build an invoice system.
input goes here
Subtotal: subtotal
Tax Rate:
Taxable: taxable
Tax: Tax
Total: Total
The code for managing the invoice page is very simple.
We define the relationship between the data:
case class Line(guid: String, name: String, price: Double, taxable: Boolean)
private object Info {
val invoices = ValueCell(List(newLine))
val taxRate = ValueCell(0.05d)
val subtotal = invoices.lift(_.foldLeft(0d)(_ + _.price))
val taxable = invoices.lift(_.filter(_.taxable).
foldLeft(0D)(_ + _.price))
val tax = taxRate.lift(taxable) {_ * _}
val total = subtotal.lift(tax) {_ + _}
}
Next we create snippets to display the data:
def subtotal(in: NodeSeq) = WiringUI.asText(in, Info.subtotal)
def taxable(in: NodeSeq) = WiringUI.asText(in, Info.taxable)
def tax(in: NodeSeq) = WiringUI.asText(in, Info.tax, JqWiringSupport.fade)
def total(in: NodeSeq) = WiringUI.asText(in, Info.total, JqWiringSupport.fade)
And hook the snippets into our view:
<div>Subtotal: <span class="lift:InvoiceWiring.subtotal">subtotal</span></div>
<div>Tax Rate: <input class="lift:InvoiceWiring.taxRate"></div>
<div>Taxable: <span class="lift:InvoiceWiring.taxable">taxable</span></div>
<div>Tax: <span class="lift:InvoiceWiring.tax">Tax</span></div>
<div>Total: <span class="lift:InvoiceWiring.total">Total</span></div>
And each time any of the cells changes, the display is automatically
updated.