That may need a slight modification. If the RequestVar is top level then a def can't exist side by side with it.
------------------------------------- David Pollak<[email protected]> wrote: On Sun, Nov 15, 2009 at 12:45 PM, DMB <[email protected]> wrote: > > I guess the underlying problem I have is that I don't know how one > object can get a reference to another. Maybe I'm just trying to use > OOP when I need to be functional, I don't know. I basically want the > time selector to be completely separate from thumbnail JSON generator > so that I could compose the two as needed. In the OOP paradigm this is > trivial - just put them into a common object, initialize them before > render, and be done with it. In Lift, I don't even know when (and how) > the classes that contain snippet functions are instantiated, let alone > how to get a reference from one to another. > You're in luck, Scala has singletons, so you never have to worry about how to access something. In a the "MyStuff.scala" file in the lib package: package com.mycode.lib import net.liftweb._ import http._ object MyWackyStuffThatNeedsToBeCalculatedOncePerRequest extends RequestVar(calculateIt) def calculateIt = {.....} Now, in your snippet or whatever code is spewing content as part of the HTTP request: import com.mycode.lib class MySnippet { def render = { val theValue = MyWackyStuffThatNeedsToBeCalculatedOncePerRequest.is <span>The value is {theValue}</span> } } Voila... no need to do anything other than reference the MyWackyStuff singleton and everything else is done for you. Does this help? Thanks, David > > What I ended up doing is I put both time selector code and JSON > generator code into the same class and made them refer to a set of > common, lazily initialized vars. This is not optimal, but I couldn't > come up with anything more elegant than this off the top of my head. > > On Nov 15, 12:08 pm, David Pollak <[email protected]> > wrote: > > On Sun, Nov 15, 2009 at 2:46 AM, DMB <[email protected]> wrote: > > > > > Here's a simple problem. I have a web page which is supposed to > > > display a gallery. > > > > > The page has two snippets in it. One of the snippets is month/year > > > selector, which takes (in the order of priority) URL parameters or > > > cookies and renders year/month selections accordingly. This month/year > > > selector also retrieves from the DB the list of available years, and > > > then for a selected year, list of available months, so if no cookies > > > or url parameters are available it picks the latest available month > > > and year. > > > > > The second part is the image viewing/browsing component itself. This > > > component is basically a static (as in, "a file on the disk") JQuery > > > script which takes all of its data from a JSON var that snippet is > > > supposed to render onto the page. The problem is, by the time its > > > rendering method is called, I must be sure that I have a valid month > > > and year selection in the picker, otherwise I don't know what to get > > > from the DB. > > > > > Coming from ASP.NET, there are several distinct phases in the page > > > lifecycle and I can always guarantee that one will finish before the > > > other, and stuff the state into my page object, or the session object, > > > or request object. > > > > > Coming from Ruby, I can stuff the date picker section and thumbnails > > > section as partials into the same view, and they will share the same > > > variables, which I will assign from the controller method, thus > > > guaranteeing the order of execution. > > > > > With Lift, my snippets are nothing but functions, and I haven't seen > > > any guarantees that one will be called before the other. How do I > > > guarantee that by the time JSON needs to be rendered I already have a > > > valid month and year selected? > > > > > I'm most likely missing something trivial here, could someone give me > > > a hint? > > > > Lift is built on Scala. Scala is good at being lazy... evaluating code > only > > when needed and only evaluating the code once. > > > > Lift has a class called RequestVar. You can use it to calculate/store a > > value during a single HTTP request/response cycle (and the calculated > value > > is retained for subsequent Ajax calls). > > > > Syntactically: > > > > object MyMonth extends RequestVar(calculateValue) > > > > def calculateValue = look at cookies or look at request params or look at > > database or this month > > > > When you call MyMonth.is the first time during the servicing of a request > > (presuming you have not explicitly set the value), calculateValue will be > > called and you can do whatever logic you want. Subsequent calls the > > MyMonth.is will return the calculated/cached value. > > > > Does that help? > > > > Thanks, > > > > David > > > > > > > > -- > > 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 > > > -- 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 [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 -~----------~----~----~----~------~----~------~--~---
