Ajax without bind

39 views
Skip to first unread message

Emmanuel Eytan

unread,
Apr 27, 2012, 11:52:41 AM4/27/12
to lif...@googlegroups.com
Hi. I'm at last adding some Ajax to my Lift project. But all the examples I find use bind. I was told previously on this forum that bind was deprecated. Is that also true of AjaxForm?

Basically, I'm looking for a button that would do something like run a function exampleFunction such as:

def render = {
  def exampleFunction(a: AMappedThing) =
    a.someProperty("someValue").save

  "ul *" #> AMappedThing.findAll().map(a => <li><p>{a.name}</p><button>Change</button></li>) &
  "the button I just created" #> onClick(exampleFunction(the_a_that_matches_each_button: AMappedThing))
}

How can I generate an Ajax button for each element found that will know which element it's attached to? I was thinking of using andThen and modifying the element I just created, but that looks messy. How do I link a Lift function to an Ajax call? Do I need actors for that?

Thanks.

Naftoli Gugenheim

unread,
Apr 27, 2012, 11:56:53 AM4/27/12
to lif...@googlegroups.com
bind and css selectors are just two different "languages" to specify a "find-replace" on xml. It shouldn't affect how you add ajax elements.
How about change the <button>... part to {SHtml.ajaxButton...} ?
Also you can nest css selectors (or Seqs of them), so if you already have the button and li/p in the template, you can do
"ul *" #> AMappedThing.findAll().map{ a =>
   "p *" #> a.name &
   "button" #> whatever...
}


--
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

Diego Medina

unread,
Apr 27, 2012, 11:58:03 AM4/27/12
to lif...@googlegroups.com
You are in luck, just last night I got a really simple sample on github
https://github.com/fmpwizard/ajaxInvoke

Feel free to ask questions about it.

Regards,

Diego

On Fri, Apr 27, 2012 at 11:52 AM, Emmanuel Eytan <eje...@gmail.com> wrote:
> --
> 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



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Emmanuel Eytan

unread,
Apr 27, 2012, 12:16:19 PM4/27/12
to lif...@googlegroups.com
Thanks! This is EXACTLY what I need. And I'm not sure I would have figured it out without your help.

I'm still missing one last part: how I do make it do Lift stuff rather than client-side stuff? How can I get Ajax to call a Lift function? Do I have to go through actors or REST for that?

Diego Medina

unread,
Apr 27, 2012, 12:21:54 PM4/27/12
to lif...@googlegroups.com
On Fri, Apr 27, 2012 at 12:16 PM, Emmanuel Eytan <eje...@gmail.com> wrote:
> Thanks! This is EXACTLY what I need. And I'm not sure I would have figured
> it out without your help.
>
> I'm still missing one last part: how I do make it do Lift stuff rather than
> client-side stuff? How can I get Ajax to call a Lift function? Do I have to
> go through actors or REST for that?
>

No actors or RESt needed here

from
http://blog.fmpwizard.com/scala-lift-custom-wizard

there is this snippet of code
https://gist.github.com/1692799

The line

info("Data confirmed!")

is code running in the lift server

so that could be the call to your

def mySuperMethod ={
println("This is called from the web and executed on the lift server")
}

Does that make sense ?
Regards,

Diego

Emmanuel Eytan

unread,
Apr 27, 2012, 12:59:56 PM4/27/12
to lif...@googlegroups.com
This is like pretty much everything in Lift: very hard to figure out and extremely easy to use. It's so incredibly simple that I can hardly remember how completely baffled by it I was fifteen minutes ago.

Yes, that is exactly what I needed and I can do what I would have done with another framework with about one twentieth of the code.

I'm so glad I picked Lift!

Diego Medina

unread,
Apr 27, 2012, 1:28:42 PM4/27/12
to lif...@googlegroups.com
Yes, once you get the Lift way, doing advance things ends up pretty
simple, of course, it takes time to get there, but this is why we have
the mailing list, to help each other get there.

Diego

Emmanuel Eytan

unread,
Apr 27, 2012, 1:48:38 PM4/27/12
to lif...@googlegroups.com
Actually, I'm have a new problem with this:

In my snippet, I have:

      ".my-button [onclick]" #> SHtml.ajaxInvoke(() => {
        SetHtml("%s-status".format(a.id.is), <span>Changed</span>) &
        () => a.myValue(Changed)
      }

And that yields the following error:

[error] /home/eeytan/verawebsite/src/main/scala/code/snippet/MySnippet.scala:34: type mismatch;
[error]  found   : () => code.model.MyModel
[error]  required: net.liftweb.http.js.JsCmd
[error]         {() => a.myValue(Changed)}
[error]             ^
[error] one error found

When it was println, Lift was okay with it, even though println is not a JsCmd, but when I try to use the ajaxInvoke to change a mapped value, Lift says that my model is not a JsCmd. I've looked at all the JsCmds in the documentation and I can't seem to be able to find one that fits.

Diego Medina

unread,
Apr 27, 2012, 2:03:15 PM4/27/12
to lif...@googlegroups.com
Change the order, first change your mapper value, then do the SetHtml

Diego

Emmanuel Eytan

unread,
Apr 27, 2012, 2:53:18 PM4/27/12
to lif...@googlegroups.com
That worked! I'm still not sure why, but I'm just glad it did. Thanks!

Diego Medina

unread,
Apr 27, 2012, 3:18:27 PM4/27/12
to lif...@googlegroups.com
Glad it worked, now to explain why :) :

println() returns Unit
There is an implicit conversion somewhere in Lift that coverts Unit to
a JsCmds.Noop
This is why println() worked fine.

But there is no conversion from what mapper returned to a JsCmd,
So by changing the order in which you do things, you just need to make
sure the last line, the last expression, returns a JsCmd. and before
it you can have as many regular scala method calls as you want.

One more thing, if you need to send two or more JsCmd, like an Alert
and a setHtml, you chain them with an &

like


Alert("Got it!") &
JsCms.SetHtml(...)

Hope that clears things out.

Diego

Emmanuel Eytan

unread,
May 1, 2012, 10:21:49 AM5/1/12
to lif...@googlegroups.com
Thanks. I've been using all of this a lot these past three days!
Reply all
Reply to author
Forward
0 new messages