The missing piece there is that I would like it to redirect to another
page if the RequestVar is not populated. With mapSnippet, it sounds
like I'd have to have a "dummy" snippet with the same shape as the
original snippet where all the snippets redirect, e.g.
class InitializedTemplateEditor(template: Template)
extends StatefulSnippet
{
val dispatch: DispatchIt = {
case "one" => doOne _
case "two" => doTwo _
...
}
...
}
class TemplateEditor /* named TemplateEditor so that HTML templates
will use this unless mapSnippet has overridden */
extends StatefulSnippet
{
val dispatch: DispatchIt = {
case "one" => doRedirect _
case "two" => doRedirect _
...
}
def doRedirect(ns: NodeSeq): NodeSeq = S.redirectTo("chooser", ()
=> { S.error("Session information lost, please choose what template to
edit again") })
}
and then some helper to map all the individual dispatches
object TemplateEditor
{
def redirectToEditor(template: Template) = {
val editor = InitializedTemplateEditor(template)
S.mapSnippet("TemplateEditor.one", editor.doOne _)
S.mapSnippet("TemplateEditor.one", editor.doOne _)
...
S.redirectTo("/editor")
}
}
which seems pretty wordy. In this case I think I'd prefer lazy vals /
boxes as state. Also, the docs for S.mapSnippet claim per-session, but
the docs for S.snippetMap (which mapSnippet uses) claim per-request,
so it might be even more onerous.
-Ross
On Aug 14, 2009, at 5:30 PM, Naftoli Gugenheim wrote:
> Not sure if I read your message closely enough, but if you want to
> redirect to a pre-initialized StatefulSnippet, one option is to
> instantiate it and use its snippet function in call to S.mapSnippet,
> in the redirect function. This will override lift's default behavior
> and use the snippet function to process the page being redirected to.
>
>
> -------------------------------------
> Ross Mellgren<[email protected]> wrote:
>
>
> I have a stateful snippet:
>
> class TemplateEditor
> extends StatefulSnippet
> {
> ...
> val template =
> (TemplateEditor.template.is
> openOr
> S.redirectTo("chooser", () => {
> S.error("Session information lost, please choose what
> template to edit again")
> }))
> ...
> }
>
> with a companion object holding a RequestVar:
>
> object TemplateEditor
> {
> /** Request var that is populated with what template is under
> edit */
> object template extends RequestVar[Box[Template]](Empty)
> ...
> }
>
> The flow is that another page with snippet chooses a template to edit,
> and then uses:
>
> S.redirectTo("/editor", () =>
> { TemplateEditor.template(Full(template)) })
>
> to stuff the template to edit into the RequestVar. I'd like for the
> state of TemplateEditor to
> use the value of the RequestVar, but not to require every link to re-
> stuff that RequestVar once
> the StatefulSnippet has been instantiated, and not require each
> snippet method to be wrapped
> with the logic to check the RequestVar and do the redirect if it's not
> populated.
>
> The problem I'm running into is that snippet instantiation is
> apparently too early to use
> S.redirectTo -- the Shortcut exception is being trapped as an
> instantiation exception and not
> being used as a redirect.
>
> It doesn't look entirely trivial to touch up LiftSession to pass this
> through, so I guess
> I'm partly soliciting feedback on whether there's a better pattern to
> use here (lazy val?) and
> partly asking if the behavior as it stands is desired.
>
> Thanks,
> -Ross
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---