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 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 -~----------~----~----~----~------~----~------~--~---
