Reputation: 24931
Programming in Lift
(Scala
) is really stressful, both of them have very scarse documentation, and the few you can find is incomplete and misleading.
Well, what I'm trying to do is to store a simple string in a SessionVar
. So, one snippet will fill the value of this string using a form and in another snippet I'm gonna show the string in session (or its default value).
What I have so far is:
The SessionVar
object:
// the SessionVar will contain a String with "Anonymous" as default value.
object myUser extends SessionVar[String]("Anonymous")
Snippet where I fill the string:
object Login extends LiftScreen {
val name = field("Name: ", "")
def finish() {
// set the SessionVar string with the string entered
myUser.set(name)
S.notice("Your name is: "+name)
}
}
Snippet where I show the string (another snippet):
// show the string in SessionVar
"Your name: " + myUser.is
...
MyUser
is the object I'm saving in session. The big question is: where do I keep my MyUser
object? I tried in the Boot.scala
and in the two snippets, but I keep getting this error: not found: value myUser
.
Where should I keep it? How should I import it? How can I make it work?
Upvotes: 3
Views: 696
Reputation: 2768
You can place your SessionVar on the same "file" as your LiftScreen, but outside the object definition.
Something like this:
package com.code.snippet
import ...
object myUser extends SessionVar[String]("Anonymous")
object Login extends LiftScreen {
val name = field("Name: ", "")
def finish() {
// set the SessionVar string with the string entered
myUser.set(name)
S.notice("Your name is: "+name)
}
}
Now, on your other snippet, assuming you have it on a different file (Which I think it is as you are using LiftScreen, but if you were using a regular snippet class you could have more than one method rendering parts of the UI. On this other file, you do need to import the object.
package com.code.snippet
import com.code.snippet.myUser
class MySnippet {
render ={
"#message" #> "Your name: " + myUser.is
}
}
You can also do it like this:
package com.code
package snippet
// notice the package split into two lines, making the import shorter.
import myUser
class MySnippet {
render ={
"#message" #> "Your name: " + myUser.is
}
}
Upvotes: 11