jahilldev
jahilldev

Reputation: 3812

SCALA Lift - AJAX form multiple values

I have the following form:

<form class="lift:form.ajax">
    <input type="hidden" class="lift:StreamIn" id="path" value="PATH" />
    <input type="submit" value="" />
</form>

Which feeds into:

object StreamIn {

    def render = SHtml.onSubmit(path => {

        StreamServer ! path

    })

}

case class StreamItem(user: String, path: String, level: String, room: String)

object StreamServer extends LiftActor with ListenerManager {

    private var streams: List[StreamItem] = Nil

    def createUpdate = streams

    override def lowPriority = {

        case stream: String if stream.length > 0 =>

            streams :+= StreamItem("James", stream, "_1", "demo-room");
            updateListeners()

    }

}

What I'm looking for is a way of passing multiple values to the StreamServer with more than one input.

So instead of the static string values "James", "_1" and "demo-room" they will be passed from the form.

Thanks in advance for any help, much appreciated :)

Upvotes: 2

Views: 680

Answers (1)

fmpwizard
fmpwizard

Reputation: 2768

Is it ok to have several input fields on your ajax form? If so, how about:

<form class="lift:form.ajax">
  <div class="lift:StreamIn">
    <input type="text" name="path" />
    <input type="text" name="user" />
    <input type="text" name="level" />
    <input type="text" name="room" />
    <input type="hidden" name="hidden" />
  </div>
</form>

Updated:

object StreamIn {
  case class StreamItem(user: String, path: String, level: String, room: String)
  def render = {
    var path= ""
    var user= ""
    var level= ""
    var room= ""
      def process(): JsCmd= {
        val message= StreamItem(user, path, level, room)
        StreamServer ! message
      }

    "name=path" #> SHtml.onSubmit(path= _ ) &
    "name=user" #> SHtml.onSubmit(user= _ ) &
    "name=level" #> SHtml.onSubmit(level= _ ) &
    "name=room" #> SHtml.onSubmit(room= _) &
    "name=hidden" #>  SHtml.hidden(process)

  }
}

And change the lowPriority for:

override def lowPriority = {
  case StreamItem(userIn, pathIn, levelIn, roomIn) => {
    streams :+= StreamItem(userIn, pathIn, levelIn, roomIn);
    updateListeners()
  }

}

I missed a few } but I hope you get the idea, you may be able to omit some fields like the room and level, if you have them available, you may be able to access them by using a RequestVar.

You can do more reading on ajax forms on the Simply Lift Book

Hope it helps

Upvotes: 3

Related Questions