Is it possible to define a function to be called when a MenuItem is selected?

69 views
Skip to first unread message

Jonathan Ferguson

unread,
May 15, 2012, 3:00:21 AM5/15/12
to lif...@googlegroups.com
Hello all, 
   I have a custom Loc in which I override supplimentalKidMenuItems to build a sub menu based on a database table.  Ideally when a MenuItem from the sub menu is clicked a function would be run allowing me to change state.  Currently I'm stuck and am thinking about using a RestHelper to trigger the state change, which seems ugly.

   override def supplimentalKidMenuItems =  CurrentUser.is match{

      case Full(u) =>

        val ts = u.things

        ts.size match {

          case 1 => Nil  

          case otherwise =>

            val x = for { t <- tsyield {MenuItem(Text(t.name.is), Text("switch/%s".format(t.id)), Nil, false, false,Nil)}

            x.toList ::: super.supplimentalKidMenuItems 

        }

      case otherwise => Nil

      }

Any pointers would be appreciated.


Thank you 
Jono

David Whittaker

unread,
May 15, 2012, 11:42:58 AM5/15/12
to lif...@googlegroups.com
Hi Jono,

What type of state are we talking about?  Does your code need to run when the menu item is clicked, or can it be invoked when the page the menu item points to is displayed?

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Jonathan Ferguson

unread,
May 15, 2012, 6:18:48 PM5/15/12
to lif...@googlegroups.com
Hi David, 
  
The code needs to run when the menu item is clicked. So the new state is available to the snippets when the page is being built. 

Jono

David Whittaker

unread,
May 15, 2012, 6:39:18 PM5/15/12
to lif...@googlegroups.com
Jono,

Could you not accomplish that through a type safe Loc that calculates a value?  Maybe this doesn't fit your particular use case but I'm thinking that a Loc[State] where the the State is calculated via a CalcValue LocParam would make the State available to the snippets when the Loc is matched.  You can then define your snippets as:

class MySnippet(val state: State) extends DispatchSnippet {
..... some transforms that use your state .....

Jonathan Ferguson

unread,
May 22, 2012, 9:40:10 PM5/22/12
to lif...@googlegroups.com
Hi David, 

Sorry for the delay, I got caught up with another project. 

I think this could be a viable solution. Do you know where there is anything documentation on type safe Loc and LocParam other than the scala docs?

I'll have a look at the scala doc and see if I can get an example working. 

Thank you 

Jono

David Whittaker

unread,
May 23, 2012, 9:29:05 AM5/23/12
to lif...@googlegroups.com
Hi Jono,

There is some info in Exploring Lift: http://exploring.liftweb.net/master/index-7.html#toc-Subsection-7.6.2 and HarryH from Foursquare put together some slides on how they use them a while back: https://docs.google.com/present/view?id=dcbpz3ck_38cj3kdbgm.  Hope that helps.

-Dave
Reply all
Reply to author
Forward
0 new messages