On Sun, Dec 6, 2009 at 5:44 AM, Alex Black <a...@alexblack.ca> wrote:

> > We made this change for Lift 1.1.  So, if you're using 1.1, the same
> > instance of a snippet should be used for a given HTTP request.
>
> Hi David, I've just switch from Lift 1.0 to Lift 1.1-M6, do you think
> this change is in M6?


Yes.

Snippet:

class HelloWorld {
  lazy val date: Box[Date] = DependencyFactory.inject[Date] // inject the
date

  val random = randomString(20)

  def howdy(in: NodeSeq): NodeSeq =
  Helpers.bind("b", in, "time" -> date.map(d => Text(d.toString)))

  def dog: NodeSeq = <b>String is {random}</b>

  def cat: NodeSeq = <b>From cat String is {random}</b>

}



View:

<lift:surround with="default" at="content">
  <h2>Welcome to your project!</h2>
       <lift:helloWorld.howdy>
        <span>Welcome to iscached at <b:time/></span>
      </lift:helloWorld.howdy>
    </p>

    <p> Dog sez <lift:HelloWorld.dog/></p>

    <p> Cat sez <lift:HelloWorld.cat/></p>

</lift:surround>


Browser:

Welcome to your project!




        Welcome to iscached at Mon Dec 07 12:26:41 PST 2009






 Dog sez String is 4G2NRM4TBGCJBYNGFCUF




 Cat sez From cat String is 4G2NRM4TBGCJBYNGFCUF

Works as advertised and has been in the code since at least M6.

Thanks,

David



> It doesn't seem to be... I have many calls to my
> snippet for a particular page, and I put a println statement in the
> body of the snippet, and it gets called 18 times each request!
>
> - Alex
>
>
> >
> >
> >
> > > I'd imagine lift could even generate a requestVar representing each
> > > snippet for the developers, allowing snippets in a given HTTP request
> > > to access each other easily.
> >
> > I don't think this is technically possible within the bounds of the type
> > system.
> >
> >
> >
> >
> >
> > > On Nov 20, 11:09 am, Ross Mellgren <dri...@gmail.com> wrote:
> > > > RequestVar is the standard way of doing this. For example
> >
> > > > object MySharedInformation {
> > > >      object myData extends RequestVar[List[Thing]](loadThings)
> > > >      //                               ^^^^^^^^^^^  ^^^^^^^^^^
> > > >      //                    Type of thing to store  How to initialize
> > > > variable first time it's accessed
> >
> > > >      private def loadThings: List[Thing] = ...
> >
> > > > }
> >
> > > > class Snippet1 {
> > > >      import MySharedInformation.myData
> > > >      def render(ns: NodeSeq): NodeSeq = {
> > > >          myData.is.map(thing => { ... })
> > > >      }
> >
> > > > }
> >
> > > > class Snippet2 {
> > > >      import MySharedInformation.myData
> > > >      ...
> >
> > > > }
> >
> > > > The lifetime of the value is during the current request processing
> and
> > > > any AJAX calls related to it.
> >
> > > > If you really want to initialize it in a snippet, then use a Box with
> > > > a RequestVar, like this:
> >
> > > > object MySharedInformation {
> > > >      object myData extends RequestVar[Box[List[Thing]]](Empty)
> >
> > > > }
> >
> > > > class LoaderSnippet {
> > > >      import MySharedInformation.myData
> > > >      def render(ns: NodeSeq): NodeSeq = {
> > > >          myData.set(Full(...))
> > > >      }
> >
> > > > }
> >
> > > > class ReaderSnippet {
> > > >      import MySharedInformation.myData
> > > >      def render(ns: NodeSeq): NodeSeq = {
> > > >          // If the data has not been loaded, default to an empty list
> > > >          val data = myData.is.openOr(Nil)
> > > >          ...
> > > >      }
> >
> > > > }
> >
> > > > class OtherReaderSnippet {
> > > >      import MySharedInformation.myData
> > > >      def render(ns: NodeSeq): NodeSeq = {
> > > >          // Do two entirely different things if the data has versus
> > > > has not been loaded
> > > >          myData.is match {
> > > >              case Full(data) => // do something when the data has
> been
> > > > loaded
> > > >              case _ => // do something when the data has not been
> loaded
> > > >          }
> > > >      }
> >
> > > > }
> >
> > > > HTH,
> > > > -Ross
> >
> > > > On Nov 20, 2009, at 10:59 AM, Alex Black wrote:
> >
> > > > > I've got a template page, say foobar.html, that makes a number of
> > > > > calls to functions in a snippet, e.g. mysnippet.foo1,
> mysnippet.foo2,
> > > > > mysnippet.foo3.
> >
> > > > > I'd like to do some initial work in foo1, e.g. retrieve some data
> and
> > > > > do some work on it, then in foo2 and foo3 display parts of that
> data.
> >
> > > > > Whats the easiest way to do this? I think I misunderstood the lift
> > > > > book:
> >
> > > > > "That means that for each request, Lift creates a new instance of
> the
> > > > > snippet class to execute. Any changes you make to instance
> variables
> > > > > will be discarded after the request is processed."
> >
> > > > > I thought this meant that for a given HTTP request, there would be
> one
> > > > > (and only one) instance of my snippet, so I could call several of
> its
> > > > > methods and they could all access the snippet's member variables,
> > > > > which would then be discarded at the end of the request.
> >
> > > > > Am I going about this wrong? should I only have one snippet
> function
> > > > > per template?
> >
> > > > > Thx
> >
> > > > > - Alex
> >
> > > > > --
> >
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "Lift" group.
> > > > > To post to this group, send email to lift...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > liftweb+unsubscr...@googlegroups.com<liftweb%2bunsubscr...@googlegroups.com>
> <liftweb%2bunsubscr...@googlegroups.com<liftweb%252bunsubscr...@googlegroups.com>
> >
> > > > > .
> > > > > For more options, visit this group athttp://
> > > groups.google.com/group/liftweb?hl=
> > > > > .
> >
> > > --
> >
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Lift" group.
> > > To post to this group, send email to lift...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > liftweb+unsubscr...@googlegroups.com<liftweb%2bunsubscr...@googlegroups.com>
> <liftweb%2bunsubscr...@googlegroups.com<liftweb%252bunsubscr...@googlegroups.com>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/liftweb?hl=.
> >
> > --
> > Lift, the simply functional web frameworkhttp://liftweb.net
> > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > Follow me:http://twitter.com/dpp
> > Surf the harmonics
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to lift...@googlegroups.com.
> To unsubscribe from this group, send email to
> liftweb+unsubscr...@googlegroups.com<liftweb%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
>
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

--

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


Reply via email to