jsFunc: Box[Call] - how to use this parameter - what value to pass into it

29 views
Skip to first unread message

riley

unread,
May 24, 2016, 5:06:23 PM5/24/16
to Lift

My previous "multiselect" usage was as follows:


SHtml.multiSelect(nameOpt, existingNames, existingNames = _, ("size" -> math.min(20, nameOpt.size).toString), ("style" -> "min-width:400px")) 


The example data was: Brad, Angelina, Morgan, Fred, Tom, Huan


Now....I am trying to replace that multiselect with the new method (myNewMultiSelect_*) that I newly created with the goal of creating an editable multiselect, but for now  I would like to know how to make a method call with appropriate values just like in the multiselect


Well, I am not clear about 2 parameters:  1 - jsFunc: Box[Call] 

 2 - func (I think this is the same as my "existingNames = _"). Not sure though. Because AFuncHolder is a sealed trait that extends List[String => Any and that is what we have in the multiselect


All I want to accomplish right now is to display the same multiselect box as before, with the following new code.


What value do you think I can pass into the jsFunc parameter - the highlighted parameter


def myNewMultiSelect_*(opts: Seq[(String, String)], deflt: Seq[String], jsFunc:Box[Call], 



         func
: S.AFuncHolder, attrs: ElemAttr*)(onSubmit: (T) => Any)(implicit f:PairStringPromoter[T]):Elem = {

         println
("Entered ajaxMultiSelectEdit")

    val optionSelect 
=

    
"""function(funcName, element) {

      |  var postData = ""

      |  var i = 0;

      |  var k = 0;

      |  for (k = 0; k < element.length; k++) {

      |   if (element[k].selected) {

      |     if (i == 0)

      |       postData = funcName + '=' + encodeURIComponent(element[k].value);

      |     else {

      |       postData = postData + '&' + funcName + '=' + encodeURIComponent(element[k].value);

      |     }

      |     i++;

      |   }

      |  }

      |  return postData;

      |}"""
.stripMargin

      

      println
("optionSelect is: " + optionSelect)

 

    
//val raw = (funcName: String, value: String) ⇒ JsRaw("'" + funcName + "=' + encodeURIComponent(" + value + ".value)")

    val raw 
= (funcName: String, value: String) => JsRaw(optionSelect + "('" +funcName + "'," + value +")")

    val key 
= S.formFuncName

    
//val key = funcName

 

    val vals 
= opts.map(_._1)




    val testFunc 
= S.LFuncHolder(in => in.filter(=> vals.contains(v)) match {case Nil =>false case xs=> func(xs)}, func.owner)




    S
.fmapFunc(S.contextFuncBuilder(testFunc)) {

    
//S.fmapFunc((testFunc)) {

      funcName 
=>

          
(attrs.foldLeft(<select multiple="multiple">

              
{opts.flatMap {case (value, text) => (<option value={value}>

               
{text}

               
</option>) % selected(deflt.contains(value))

               }}</
select>)(% _)) %

              
("onchange" -> (jsFunc match {

               
//(jsFunc match {

                  
case Full(f) => JsCrVar(key, JsRaw("this")) &net.liftweb.http.js.JsCmds.jsExpToJsCmd(SHtml.makeAjaxCall(raw(funcName,"this"))) 

                  
case _ => net.liftweb.http.js.JsCmds.jsExpToJsCmd(SHtml.makeAjaxCall(raw(funcName,"this"))) //def makeAjaxCall(in: JsExp): JsExp

              
}))

              

    
}

    

}

Antonio Salazar Cardozo

unread,
May 24, 2016, 10:54:11 PM5/24/16
to Lift
existingNames = _ actually goes where the AFuncHolder goes. AFuncHolder
is a Lift container for a Scala function, that is tracked inside the Lift session. The
Box[Call] parameter, on the other hand, is an optional parameter that can take
a JavaScript function call (represented in the Scala world by the Lift JsExp subclass
`Call`). I believe this is the onChange handler that is invoked on the client when an
element is selected, if I remember correctly.
Thanks,
Antonio

riley

unread,
May 26, 2016, 3:48:34 PM5/26/16
to Lift
Ah, the onChange handler. That could be it. Its purpose has been eluding me so far, until now.

Thanks
Riley
Reply all
Reply to author
Forward
0 new messages