Ah, that's interesting (I haven't used Wicket). The reason that the link function executes in the scope of its definition is that it's a closure, so it "captures" its scope. In general, closures are the foundation for a lot of Lift functionality. The same mechanism is what makes ajax and comet so darn simple to use in Lift.
Derek On Mon, May 18, 2009 at 6:23 PM, Willis Blackburn < [email protected]> wrote: > > Thanks! That helps a lot. > > It took me a few minutes to realize that when the user clicks the link > to editAcct, he will be directed to the account edit page, but the > link's function will still be executed in the context of the snippet > that generated it. It's a little different in Wicket: the link just > invokes the onClick function, and it's up to the application to > redirect to the new page. > > W > > > On May 18, 4:14 pm, Derek Chen-Becker <[email protected]> wrote: > > There are two main approaches to this, both of which are covered in the > Lift > > book that you've ordered (thanks!). The first is to use a > StatefulSnippet. > > This lets you use the same snippet class instance, so any member > variables > > are still there when the next page hit comes. You can see an example of > this > > in the AddEntry class in the PocketChange app: > > > > http://github.com/tjweir/pocketchangeapp/blob/1f97c96c9a8e3ac4c86f88d... > > > > This a very simple example, so please look at the API docs for > > StatefulSnippet for more details. The other approach (which I prefer) is > to > > use RequestVars. Combined with the SHtml.link method, you can do > something > > very similar to what you're doing in Wicket. Check out the Accounts > snippet > > in PocketChange: > > > > http://github.com/tjweir/pocketchangeapp/blob/1f97c96c9a8e3ac4c86f88d... > > > > Lines 35-49 look like: > > > > User.currentUser.map({user => > > user.accounts.flatMap({acct => > > bind("acct", chooseTemplate("account", "entry", xhtml), > > "name" -> Text(acct.name.is), > > "description" -> Text(acct.description.is), > > "actions" -> { link("/manage", () => acct.delete_!, > > Text("Delete")) ++ Text(" ") ++ > > link("/editAcct", () => > > currentAccountVar(acct), Text("Edit")) }) > > }) > > }) openOr Text("You're not logged in") > > } > > > > object currentAccountVar extends RequestVar[Account]({ > > Account.create.owner(User.currentUser.open_!) > > }) > > def currentAccount = currentAccountVar.is > > > > In particular, the line > > > > link("/editAcct", () => currentAccountVar(acct), Text("Edit")) }) > > > > Sets up the link so that it loads the current account into the > > currentAccountVar RequestVar when the link is clicked. That allows you to > > pass info along with links (other transitions are possible). S.redirectTo > > has an overload that takes a function, too, so you could similarly pass > > state that way. > > > > Derek > > > > On Mon, May 18, 2009 at 9:04 AM, Willis Blackburn < > > > > [email protected]> wrote: > > > > > Hi all. I just picked up Lift and Scala a couple of weeks ago. I have > > > been using mostly Wicket for the last couple of years. I have ordered > > > David's Scala book and a Lift book from Amazon, but they're not here > > > yet, so I am hoping that someone can help me with some conceptual > > > issues. > > > > > In Wicket, pages are components. Something that I like about Wicket is > > > how easy it is to create a link to a related page: I can just pass > > > state from the current page to the constrictor of the new page. For > > > example, given a list of products, I can create a link to a product > > > detail page like so: > > > > > new Link("mylink") { > > > void onClick() { > > > setResponsePage(new ProductPage(product)); > > > } > > > } > > > > > Basically the product page is returned from the link click. What is > > > the recommended way of doing this in Lift? Should I just create a > > > regular link to a URL and pass the product ID as a parameter? Or is > > > there a better way which will allow me to avoid reloading the product, > > > checking privileges, and so on? > > > > > Thank you, > > > Willis > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
