David,
This is an excellent example of something that I was extremely
frustrated with the first week learning Scala and Lift. I suspect it
would make a great edition to the wiki, FAQ, and book as well; and
save us newbs some time and heartache. If I could make one additional
suggestion, it would be to also give an example of using a foreign key
in a Select input (fairly common case). Having this close by the
foreign key example would be handy.
Something like:
def add(form: NodeSeq) = {
val refersto : RefersTo = RefersTo.create
val allTargets = Target.allTargets.map(targ => (targ.id.toString,
targ.name.toString))
def doBind(form: NodeSeq) = {
bind("refto", form,
"name" -> refersto.name.toForm,
"taget" -> select(allTargets, Full
(refersto.target.toString), refersto.target.setFromAny),
"submit" -> submit("Add Event", checkAndSave)
)
}
def checkAndSave(): Unit = {
event.validate match {
case Nil => event.save ; S.notice("Added: " +
event.name)
case xs => S.error(xs) ; S.mapSnippet("Events.add",
doBind)
}
}
doBind(form);
}
Thanks for all your work on Lift. I look forward to being more able
to contribute to Lift in the future. Great question Hannes.
-Grant
On Aug 19, 11:47 am, David Pollak <[email protected]>
wrote:
>
> First, please take a look
> athttp://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-clas...
> It's a discussion about Option, which provides a subset of what Lift's Box
> provides. It's also based on Scala 2.3 syntax... but you'll get the idea of
> what Option/Box is all about.
>
> So, if you've got a LongKeyedMapper that is a target of a foreign key
> reference from another Mapper class:
>
> class Taget extends LongKeyedMapper[Target] with IdPK {
> ...
>
> object name extends MappedString(this, 64)
>
> }
>
> class RefersTo extends Mapper[RefersTo] {
> object target extends MappedLongForeignKey(this, Target)
>
> }
>
> So, if I've got a RefersTo instance and I want to get the name from the
> target foreign key reference:
>
> val rt: RefersTo = ...
> val target: Box[Target] = rt.target.obj // load the FK reference, if you can
> val nameBox: Box[String] = target.map(_.name.is) // you need the "is" part
> so you get the String, not the MappedString
> val name: String = nameBox openOr "N/A"
>
> But we can shorten the whole thing to:
>
> val name = rt.target.obj.map(_.name.is) openOr "N/A"
>
> In general, when you're starting out, write out the complex expressions as
> separate lines with the types explicitly declared. Then refactor over and
> over to make the code more concise. It might be worthwhile to leave the
> original code in a comment so you can remind yourself the "exploded" view of
> the expression.
>
>
>
> > thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---