The problem with the Box is unwrapping it in every snippet method,
when I really want to guarantee that if the StatefulSnippet is
instantiated, it has these parameters. Moreover, I have additional
pieces of state which need to be initialized based on the other
parameters.
For this particular snippet, I put a hack into place based on your
note about the dispatch function... there's a risk of NPE, but it
should work okay:
class TemplateEditor
extends StatefulSnippet
{
val dispatch: DispatchIt = {
case _ if (template == null) => S.redirectTo("chooser", () => {
S.error("Session information lost, please choose what
template to edit again")
})
...
}
val template = TemplateEditor.template.is.openOr(null) // eww
lazy val imageFuncBinding = S.fmapFunc[String](() =>
{ TemplateEditor.template(Full(template)) })(identity)
}
the key part is that I needed to make derived state lazy (so that if
it happens to be null I don't NPE early) and just ensure that no calls
will enter that are before dispatch gets called. Seems a bit ugly, but
at least I don't have to pass all my state down to the snippet methods
by hand.
You refer to Request, Client, etc, which don't mean anything to me.
What are you talking about?
-Ross
On Aug 14, 2009, at 6:27 PM, Naftoli Gugenheim wrote:
> I think it's a mistake, and it's per request. Could someone confirm?
> In any case, I don't see why you need a separate template class or a
> RequesVar. Just use an Option or Box var in your snippet, and let it
> redirect whenever it's None/Empty. You can put the check in the
> dispatch partial function, in the snippet functions, or in a def
> dispatch that if it's empty returns a case _ => redirectTo ... PF.
> I'm not sure if I'm missing something, but I've been doing something
> similar (if not more complex):
> When editing a Request you can go to the Client edit or search page,
> both of which return to the Request screen with its state intact.
> The edit page is populated with the current Client. The search page
> can also branch to the client edit page, either editing an existing
> client or editing a new unsaved client based on the search criteria.
> Editing returns to the search page while saving a new Client returns
> to the Request page. This is all using StatefulSnippets defined once
> and mapSnippet etc., except for ClientSearch which isn't a snippet
> and so uses RequestVars.
>
>
>
> -------------------------------------
> Ross Mellgren<[email protected]> wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---