[Lift] Re: RequestVar not Persisting
Ok, it was quite easy once I thought about it some more. For anyone who comes along this later, here is my solution: def addUsers(xhtml: NodeSeq): NodeSeq = { val possible = User.findAll().map(user => (user.id.toString, user.name)) val current = role.users.map(user => user.id.toString) var updated: List[Long] = Nil val r = for { id <- S.param("id") num <- Full(id.toLong) } yield { Role.find(By(Role.id, num)) } def loadUsers(ids: List[String]) = { updated = ids.map(_.toLong) } def processUsers() = { println(updated) println(r) } bind("entry", xhtml, "users" -> SHtml.multiSelect(possible, current, loadUsers(_)), "submit" -> SHtml.submit("Update", processUsers) ) } Peter On Sep 4, 4:36 pm, Peter Robinett wrote: > Thanks guys, I knew I was missing something simple. So how do I then > do I make it so I have the Role id at the end of the form submission > process? > > Thanks, > Peter > > On Sep 4, 2:58 pm, Kris Nuttycombe wrote: > > > While your "updated" list will be modified by the loadUsers closure, > > the for comprehension will only be executed on the initial rendering > > of the snippet, not as part of the execution of your loadUsers or > > processUsers callbacks. So the state of the RequestVar will never be > > reset. > > > Kris > > > On Fri, Sep 4, 2009 at 2:01 PM, Peter Robinett > > wrote: > > > > I'm trying to use a RequestVar to keep the Mapper model that I'm > > > editing throughout the lifetime of a simple form. Specifically, I'm > > > adding Users to a Role. The problem is, the RequestVar seems to get > > > recreated at every step and, I must admit, it's driving me crazy and > > > I'd appreciate some help. > > > > Here's my snippet code: > > > object currentRole extends RequestVar[Role](new Role) > > > > def addUsers(xhtml: NodeSeq): NodeSeq = { > > > val possible = User.findAll().map(user => (user.id.toString, > > > user.name)) > > > val current = role.users.map(user => user.id.toString) > > > var updated: List[Long] = Nil > > > > for { > > > id <- S.param("id") > > > num <- Full(id.toLong) > > > e <- Role.find(By(Role.id, num)) > > > } { > > > println(currentRole) > > > currentRole(e) > > > println(currentRole) > > > } > > > > def loadUsers(ids: List[String]) = { > > > updated = ids.map(_.toLong) > > > } > > > > def processUsers() = { > > > println(updated) > > > println(currentRole) > > > } > > > > bind("entry", xhtml, > > > "users" -> SHtml.multiSelect(possible, current, loadUsers(_)), > > > "submit" -> SHtml.submit("Update", processUsers) > > > ) > > > } > > > > And the template: > > > > > > > > > > > > > > > > > > > > > > And my STDOUT: > > > com.equalnetworks.model.Role={Parent > > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > > com.equalnetworks.model.Role={Parent > > > Role:=NULL,datacenter=NULL,Name:=Customers,id=2,Delete:=false,Update:=false,Create:=false,Read:=false} > > > INFO - Service request (GET) /roles/memberships/ took 87 Milliseconds > > > INFO - Service request (GET) /classpath/jquery.js took 7 Milliseconds > > > INFO - Service request (GET) /images/ajax-loader.gif took 6 > > > Milliseconds > > > List(1, 2) > > > com.equalnetworks.model.Role={Parent > > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > > > As you can see, I'm using a for comprehension to take a request > > > parameter (e.g. /roles/memerships/?id=2) and get a Role, which I then > > > successfully assign to my currentRole RequestVar (see how id is first > > > -1 and then 2). Unfortunately once I submit the form processUsers is > > > called and currentRole again has an id of -1. Why is this? > > > > Thanks in advance, > > > Peter Robinett --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@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 -~--~~~~--~~--~--~---
[Lift] Re: RequestVar not Persisting
Thanks guys, I knew I was missing something simple. So how do I then do I make it so I have the Role id at the end of the form submission process? Thanks, Peter On Sep 4, 2:58 pm, Kris Nuttycombe wrote: > While your "updated" list will be modified by the loadUsers closure, > the for comprehension will only be executed on the initial rendering > of the snippet, not as part of the execution of your loadUsers or > processUsers callbacks. So the state of the RequestVar will never be > reset. > > Kris > > On Fri, Sep 4, 2009 at 2:01 PM, Peter Robinett wrote: > > > I'm trying to use a RequestVar to keep the Mapper model that I'm > > editing throughout the lifetime of a simple form. Specifically, I'm > > adding Users to a Role. The problem is, the RequestVar seems to get > > recreated at every step and, I must admit, it's driving me crazy and > > I'd appreciate some help. > > > Here's my snippet code: > > object currentRole extends RequestVar[Role](new Role) > > > def addUsers(xhtml: NodeSeq): NodeSeq = { > > val possible = User.findAll().map(user => (user.id.toString, > > user.name)) > > val current = role.users.map(user => user.id.toString) > > var updated: List[Long] = Nil > > > for { > > id <- S.param("id") > > num <- Full(id.toLong) > > e <- Role.find(By(Role.id, num)) > > } { > > println(currentRole) > > currentRole(e) > > println(currentRole) > > } > > > def loadUsers(ids: List[String]) = { > > updated = ids.map(_.toLong) > > } > > > def processUsers() = { > > println(updated) > > println(currentRole) > > } > > > bind("entry", xhtml, > > "users" -> SHtml.multiSelect(possible, current, loadUsers(_)), > > "submit" -> SHtml.submit("Update", processUsers) > > ) > > } > > > And the template: > > > > > > > > > > > > > > > And my STDOUT: > > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=Customers,id=2,Delete:=false,Update:=false,Create:=false,Read:=false} > > INFO - Service request (GET) /roles/memberships/ took 87 Milliseconds > > INFO - Service request (GET) /classpath/jquery.js took 7 Milliseconds > > INFO - Service request (GET) /images/ajax-loader.gif took 6 > > Milliseconds > > List(1, 2) > > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > > As you can see, I'm using a for comprehension to take a request > > parameter (e.g. /roles/memerships/?id=2) and get a Role, which I then > > successfully assign to my currentRole RequestVar (see how id is first > > -1 and then 2). Unfortunately once I submit the form processUsers is > > called and currentRole again has an id of -1. Why is this? > > > Thanks in advance, > > Peter Robinett --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@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 -~--~~~~--~~--~--~---
[Lift] Re: RequestVar not Persisting
On Fri, Sep 4, 2009 at 1:01 PM, Peter Robinett wrote: > > I'm trying to use a RequestVar to keep the Mapper model that I'm > editing throughout the lifetime of a simple form. Specifically, I'm > adding Users to a Role. The problem is, the RequestVar seems to get > recreated at every step and, I must admit, it's driving me crazy and > I'd appreciate some help. > > Here's my snippet code: > object currentRole extends RequestVar[Role](new Role) > > def addUsers(xhtml: NodeSeq): NodeSeq = { >val possible = User.findAll().map(user => (user.id.toString, > user.name)) >val current = role.users.map(user => user.id.toString) >var updated: List[Long] = Nil > >for { > id <- S.param("id") > num <- Full(id.toLong) > e <- Role.find(By(Role.id, num)) >} { > println(currentRole) > currentRole(e) > println(currentRole) >} > >def loadUsers(ids: List[String]) = { > updated = ids.map(_.toLong) >} > >def processUsers() = { > println(updated) > println(currentRole) >} > >bind("entry", xhtml, > "users" -> SHtml.multiSelect(possible, current, loadUsers(_)), > "submit" -> SHtml.submit("Update", processUsers) >) > } > > And the template: > > > > > > > > And my STDOUT: > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=Customers,id=2,Delete:=false,Update:=false,Create:=false,Read:=false} > INFO - Service request (GET) /roles/memberships/ took 87 Milliseconds > INFO - Service request (GET) /classpath/jquery.js took 7 Milliseconds > INFO - Service request (GET) /images/ajax-loader.gif took 6 > Milliseconds > List(1, 2) > com.equalnetworks.model.Role={Parent > > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > As you can see, I'm using a for comprehension to take a request > parameter (e.g. /roles/memerships/?id=2) and get a Role, which I then > successfully assign to my currentRole RequestVar (see how id is first > -1 and then 2). Unfortunately once I submit the form processUsers is > called and currentRole again has an id of -1. Why is this? > Because the second time around is not part of the first request and the scope of the RequestVar is the current request. > > Thanks in advance, > Peter Robinett > > > -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Git some: http://github.com/dpp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@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 -~--~~~~--~~--~--~---
[Lift] Re: RequestVar not Persisting
While your "updated" list will be modified by the loadUsers closure, the for comprehension will only be executed on the initial rendering of the snippet, not as part of the execution of your loadUsers or processUsers callbacks. So the state of the RequestVar will never be reset. Kris On Fri, Sep 4, 2009 at 2:01 PM, Peter Robinett wrote: > > I'm trying to use a RequestVar to keep the Mapper model that I'm > editing throughout the lifetime of a simple form. Specifically, I'm > adding Users to a Role. The problem is, the RequestVar seems to get > recreated at every step and, I must admit, it's driving me crazy and > I'd appreciate some help. > > Here's my snippet code: > object currentRole extends RequestVar[Role](new Role) > > def addUsers(xhtml: NodeSeq): NodeSeq = { > val possible = User.findAll().map(user => (user.id.toString, > user.name)) > val current = role.users.map(user => user.id.toString) > var updated: List[Long] = Nil > > for { > id <- S.param("id") > num <- Full(id.toLong) > e <- Role.find(By(Role.id, num)) > } { > println(currentRole) > currentRole(e) > println(currentRole) > } > > def loadUsers(ids: List[String]) = { > updated = ids.map(_.toLong) > } > > def processUsers() = { > println(updated) > println(currentRole) > } > > bind("entry", xhtml, > "users" -> SHtml.multiSelect(possible, current, loadUsers(_)), > "submit" -> SHtml.submit("Update", processUsers) > ) > } > > And the template: > > > > > > > > And my STDOUT: > com.equalnetworks.model.Role={Parent > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > com.equalnetworks.model.Role={Parent > Role:=NULL,datacenter=NULL,Name:=Customers,id=2,Delete:=false,Update:=false,Create:=false,Read:=false} > INFO - Service request (GET) /roles/memberships/ took 87 Milliseconds > INFO - Service request (GET) /classpath/jquery.js took 7 Milliseconds > INFO - Service request (GET) /images/ajax-loader.gif took 6 > Milliseconds > List(1, 2) > com.equalnetworks.model.Role={Parent > Role:=NULL,datacenter=NULL,Name:=,id=-1,Delete:=false,Update:=false,Create:=false,Read:=false} > > As you can see, I'm using a for comprehension to take a request > parameter (e.g. /roles/memerships/?id=2) and get a Role, which I then > successfully assign to my currentRole RequestVar (see how id is first > -1 and then 2). Unfortunately once I submit the form processUsers is > called and currentRole again has an id of -1. Why is this? > > Thanks in advance, > Peter Robinett > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@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 -~--~~~~--~~--~--~---