Skip to content
virenw edited this page Mar 8, 2014 · 4 revisions

In this part 2 of the bookstore application we will define the data models and create snippets and views to support adding and viewing the data.

DataModel

The data model consists of the following entities, Author, Publisher and Book.

Note regarding Snippets

There is problem with Snippets not being found with HTML5. The HTML5 parser and one of your snippets is rendering with a "Class Not Found" error. The solution is to switch to the designer-friendly snippet invocation mechanism. For example:

<div data-lift="HellowWorld.howdy"></div>

LiftScreen - allows us to collect user input, validate it and serve a response based on user input (, just like a form). An action function finish is executed upon submission of the form.

Add Authors

We have a LiftScreen called AuthorScreen which allow us to create/add a author. In our AuthorScreen we want to show only selected fields, so we use,

addFields(() => author.is.name)

if we wanted to show all fields we could use,

addFields(() => author.is)

but this would also show the id field which not what we want. In addition to this LiftScreen we created a add_author.html which contains the LiftScreen.

List Authors

We will list the authors in a table containing 3 columns, Name, Age and Birthday. For this we have created a snippet called Authors with the following list method,

//replace function (#>) is a CSS selector,
//On the right side of the #> symbol is the content that you want to bind to that selector on the left side
def list = "tr" #> Author.list.map( author => ".name" #> author.name & ".age" #> author.age.get
    & ".bday" #> birthdayToString(author.birthday))

birthdayToString is a private method used to convert the OptionalDateTimeField to a string.

The html which uses this snippet is called list_authors.html and contains an html table.

Add publisher and list publisher is very similar to add authors and list authors, so we can skip it for now.

Add Books

To add a new book we have to specify a name, genre, secondary genre (optional), author, publisher and year of publication. all fields other than author and publisher are textfields. These 2 fields are drop-down lists. Lets use the FormBuilderLocator, just so we get some experience on using it. Implementing a FormBuilderLocator and appending that to LiftRules.appendGlobalFormBuilder allow us to render customized fields in several screens without duplicating code. This has to be implemented in Book.scala. In our case adding the following to Boot.scala will display authors in a drop-down list,

LiftRules.appendGlobalFormBuilder(FormBuilderLocator[List[Author]](
  (authors, setter) => SHtml.select(authors.map(a => (a.id.toString, a.name.toString())), Empty, v => println(v))
  ))

When LiftScreen is presented with a List[Author] type, it will automatically display it as a drop-down list.

Clone this wiki locally