On Mon, Dec 7, 2009 at 12:50 PM, Alex Black <a...@alexblack.ca> wrote:
> Hi David, > > I just tried something similar out. > 1. I created a blank 1.0 lift project > How about starting with a Lift 1.1 project? > 2. I updated the pom file to reference lift-1.1M6 > Did you update the version of Scala? > 3. I added a line to the hello world snippet in its body: > println("Constructor") > 4. I modified index.html to call the snippet twice. > 5. I then launched the web server (mvn jetty:run) and accessed the > main page once via a browser > > The line println("Constructor") gets called twice, I expected it just > to get called once. > > And Lift may choose to instantiate a snippet instance more than once for a given request. But, there is just one instance used across all the requests to the named snippet for the balance of the call. > Am I doing something wrong? Your test is not exactly the same as the > problem I described, I described the constructor getting called 18 > times, I haven't yet verified whether or not the data is shared. > What is the issue with the constructor being called repeatedly? > > and here is the output: > > [INFO] Started Jetty Server > [INFO] Starting scanner at interval of 5 seconds. > Constructor > Constructor > INFO - Service request (GET) / took 393 Milliseconds > INFO - Service request (GET) /classpath/jquery.js took 39 Milliseconds > INFO - Service request (GET) /ajax_request/ > liftAjax.js;jsessionid=11x3vru6uimmg took 11 Milliseconds > INFO - Service request (GET) /favicon.ico took 6 Milliseconds > INFO - Service request (POST) /ajax_request/F318697140471TRN/ took 11 > Milliseconds > INFO - Service request (POST) /ajax_request/F318697140471TRN/ took 1 > Milliseconds > > Snippet code: > > package com.liftcode.hello.snippet > > class HelloWorld { > println("Constructor") > > def howdy = <span>Welcome to hello-lift at {new > _root_.java.util.Date}</span> > } > > index.html: > > <lift:surround with="default" at="content"> > <h2>Welcome to your project!</h2> > <p><lift:helloWorld.howdy /></p> > <p><lift:helloWorld.howdy /></p> > </lift:surround> > > Here is the pom.xml: > > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// > www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:// > maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> > <modelVersion>4.0.0</modelVersion> > <groupId>com.liftcode.hello</groupId> > <artifactId>hello-lift</artifactId> > <version>1.0-SNAPSHOT</version> > <packaging>war</packaging> > <name>hello-lift</name> > <inceptionYear>2007</inceptionYear> > <properties> > <scala.version>2.7.3</scala.version> > </properties> > > <repositories> > <repository> > <id>scala-tools.org</id> > <name>Scala-Tools Maven2 Repository</name> > <url>http://scala-tools.org/repo-releases</url> > </repository> > </repositories> > > <pluginRepositories> > <pluginRepository> > <id>scala-tools.org</id> > <name>Scala-Tools Maven2 Repository</name> > <url>http://scala-tools.org/repo-releases</url> > </pluginRepository> > </pluginRepositories> > > <dependencies> > <dependency> > <groupId>org.scala-lang</groupId> > <artifactId>scala-library</artifactId> > <version>${scala.version}</version> > </dependency> > <dependency> > <groupId>net.liftweb</groupId> > <artifactId>lift-util</artifactId> > <version>1.1-M6</version> > </dependency> > <dependency> > <groupId>net.liftweb</groupId> > <artifactId>lift-webkit</artifactId> > <version>1.1-M6</version> > </dependency> > <dependency> > <groupId>javax.servlet</groupId> > <artifactId>servlet-api</artifactId> > <version>2.5</version> > <scope>provided</scope> > </dependency> > <dependency> > <groupId>junit</groupId> > <artifactId>junit</artifactId> > <version>4.5</version> > <scope>test</scope> > </dependency> > <dependency> > <groupId>org.mortbay.jetty</groupId> > <artifactId>jetty</artifactId> > <version>[6.1.6,)</version> > <scope>test</scope> > </dependency> > <!-- for LiftConsole --> > <dependency> > <groupId>org.scala-lang</groupId> > <artifactId>scala-compiler</artifactId> > <version>${scala.version}</version> > <scope>test</scope> > </dependency> > </dependencies> > > <build> > <sourceDirectory>src/main/scala</sourceDirectory> > <testSourceDirectory>src/test/scala</testSourceDirectory> > <plugins> > <plugin> > <groupId>org.scala-tools</groupId> > <artifactId>maven-scala-plugin</artifactId> > <executions> > <execution> > <goals> > <goal>compile</goal> > <goal>testCompile</goal> > </goals> > </execution> > </executions> > <configuration> > <scalaVersion>${scala.version}</scalaVersion> > </configuration> > </plugin> > <plugin> > <groupId>org.mortbay.jetty</groupId> > <artifactId>maven-jetty-plugin</artifactId> > <configuration> > <contextPath>/</contextPath> > <scanIntervalSeconds>5</scanIntervalSeconds> > </configuration> > </plugin> > <plugin> > <groupId>net.sf.alchim</groupId> > <artifactId>yuicompressor-maven-plugin</artifactId> > <executions> > <execution> > <goals> > <goal>compress</goal> > </goals> > </execution> > </executions> > <configuration> > <nosuffix>true</nosuffix> > </configuration> > </plugin> > <plugin> > <groupId>org.apache.maven.plugins</groupId> > <artifactId>maven-eclipse-plugin</artifactId> > <configuration> > <downloadSources>true</downloadSources> > <excludes> > <exclude>org.scala-lang:scala-library</exclude> > </excludes> > <classpathContainers> > > <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</ > classpathContainer> > </classpathContainers> > <projectnatures> > <java.lang.String>ch.epfl.lamp.sdt.core.scalanature</ > java.lang.String> > <java.lang.String>org.eclipse.jdt.core.javanature</ > java.lang.String> > </projectnatures> > <buildcommands> > <java.lang.String>ch.epfl.lamp.sdt.core.scalabuilder</ > java.lang.String> > </buildcommands> > </configuration> > </plugin> > </plugins> > </build> > <reporting> > <plugins> > <plugin> > <groupId>org.scala-tools</groupId> > <artifactId>maven-scala-plugin</artifactId> > <configuration> > <scalaVersion>${scala.version}</scalaVersion> > </configuration> > </plugin> > </plugins> > </reporting> > </project> > > > > On Dec 7, 3:28 pm, David Pollak <feeder.of.the.be...@gmail.com> wrote: > > 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> > > > > > <liftweb%2bunsubscr...@googlegroups.com<liftweb%252bunsubscr...@googlegroups.com> > <liftweb%252bunsubscr...@googlegroups.com<liftweb%25252bunsubscr...@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> > > > > > <liftweb%2bunsubscr...@googlegroups.com<liftweb%252bunsubscr...@googlegroups.com> > <liftweb%252bunsubscr...@googlegroups.com<liftweb%25252bunsubscr...@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> > <liftweb%2bunsubscr...@googlegroups.com<liftweb%252bunsubscr...@googlegroups.com> > > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/liftweb?hl=en. > > > > -- > > 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.