> Here's some code to serve an image out of the database.
What is the preferred way to dynamically serve content from any url,
preferrably directly from a stateful snippet? After boot time, you
cannot call LiftRules.dispatch.prepend(...), so it seems the only way
is to store content in a singleton. If this is the only way, is there
a way to add a callback to run after the content has been served (so
that I can remove it afterwards)? Code samples much appreciated :)
Here's an example of how I was hoping to do this:
class DynamicContentService extends StatefulSnippet {
override def dispatch = {
case "content" => content _
}
def content(x: NodeSeq): NodeSeq = {
bind("content", xhtml,
"somefield" -> text(...),
"submit" -> ajaxSubmit("Download", () => {
val response:LiftResponse = createResponse(createContent)
val url:String = urlFor(response)
S.serve(url, response, S.unserve(ur))
JsRaw("download(url)")
}))
}
}
... where S.serve is an imaginary function that takes a String for an
url, a LiftResponse to serve, and a callback after response has been
served.
Regards, Eirik
On Mar 26 2009, 1:14 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> On Wed, Mar 25, 2009 at 2:38 PM, Thomas Rynne <thomas.ry...@gmail.com>wrote:
>
>
>
>
>
> > Hi,
> > I want to dynamically generate images (actually a graph). I could
> > write a seperate servlet for this but I'd like easy access to the
> > Mapper classes and the logged in User etc so would prefer to write it
> > as a method in lift.
>
> > Is there a hook for this? I suppose I essentially want direct acccess
> > to the httpresponse.
>
> > I found some discussion of this here:
>
> >http://groups.google.com/group/liftweb/browse_thread/thread/9d6f61f69...
>
> > but I don't know if it was ever implemented, and need some pointers/
> > documentation if it has been.
>
> > thanks for any advice,
> > Thomas
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890
Something like
LiftRules.dispatch.prepend(MyServer.dispatch)
object MyServer {
private var resources: Map[List[String], String] = Map.empty
def serve(path: List[String], body: String): Unit = resources += ((path -> body))
val dispatch = resources andThen { body => InMemoryResponse(body.getBytes, Nil, Nil, 200) }
}
Now this doesn't handle expiring/deleting things, and incorrectly converts the body string to bytes using platform default encoding, and doesn't have a callback and isn't threadsafe, but I'm typing this on an iPad so hopefully it'll give you some ideas as to what's possible.
There's a couple different strategies by the way. I know DPP has been working on a CMS module which does something like this and he's previously written an example called WikiStuff that also does something similar -- http://github.com/lift/lift/blob/master/examples/example/src/main/scala/net/liftweb/example/lib/WikiStuff.scala
HTH,
-Ross
> --
> You received this message because you are subscribed to the Google Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
>
Sorry, I was unable to reply to the thread so I'm cc'ing
lif...@googlegroups.com
What is the preferred way to dynamically serve content from any url,
> Here's some code to serve an image out of the database.
preferrably directly from a stateful snippet?
After boot time, you
cannot call LiftRules.dispatch.prepend(...), so it seems the only way
is to store content in a singleton.
If this is the only way, is there
a way to add a callback to run after the content has been served (so
that I can remove it afterwards)? Code samples much appreciated :)
What are you specifically trying to do? What's your use case?
--
https://www.assembla.com/spaces/liftweb/wiki?id=liftweb&wiki_id=REST_Web_Services
Essentially you are making a callable URL as it will be invoked by the client side JS. Perhaps its stateless, perhaps it isnt... either way, dispatching is the way to go here.
Cheers, Tim