List the many of a onetomany?

58 views
Skip to first unread message

MichielQ

unread,
Jun 26, 2011, 1:35:54 PM6/26/11
to Lift
Hi all

I'm totally new to Lift (as well as Scala) and using the available
books and documentation online I'm developing my first app. So please
bear with me for my simplistic questions.

I'm trying to show an entity with a list of it's onetomany
relationship. Here's the code I have:

def listAll(xhtml: NodeSeq) = Serial.findAll.flatMap { a =>
bind("serie", xhtml,
"name" -> a.name,
"episodes" -> a.episodes.map(_.name.toString).mkString(", ")
)}

<ul class="lift:Series.listAll">
<li><serie:name /> (<serie:episodes>)</li>
</ul>

The serie:name shows up but the episodes don't.

Can someone please tell me what .map(_.name.toString).mkString(", ")
does and how I can show this in the html?

Naftoli Gugenheim

unread,
Jun 26, 2011, 10:23:35 PM6/26/11
to lif...@googlegroups.com
When you call map on a sequence, you pass it a function that takes in an old element and returns a new element, and map applies the function to give you a new sequence of the elements resulting from the function.
In this case, you're calling map on a.episodes which is a Seq (more specifically a Buffer) of mapper entities. The function you're passing map is _.name.toString, which means take an episode entity, get its name field, and call toString on that. So map will return a Seq[String] --- the names of all the episodes.
When you call mkString on a sequence, it concatenates all the elements into one string. You can optionally give mkString a delimiter to put between elements in the resulting string, as well as a prefix string and suffix string. In this case you're just giving it a delimiter.

Anyway, did you notice that the episode tag is not a void tag, and is not closed? Try adding a slash.



--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Reply all
Reply to author
Forward
0 new messages