Form submit and Ajax

9 views
Skip to first unread message

greekscala

unread,
Dec 29, 2009, 10:44:03 AM12/29/09
to Lift
Hello,

In a previous post I had the problem that my form was not send with
ajax.
With the help of Marius and Timothy I got it working.
(http://groups.google.com/group/liftweb/browse_thread/thread/
d052ed7191561ef1)

I have to questions.

Now I want, after the form is submittet, to fetch an object from my DB
do something
and update the site.

I tried the following:
def add(xml: NodeSeq): NodeSeq = {

def addBookmark(): JsCmd = {
// fetch the object from DB

JsCmds.SetHtml(WebHelper.MAIN_DIV_ID,
TemplateFinder.findAnyTemplate
(WebHelper.bookmarkEdit).open_!)
}

SHtml.ajaxForm(
bind("bookmark", xml,
"url" -> SHtml.text(url, url = _),
"submit" -> SHtml.submit("add", addBookmark)
), JsCmds._Noop, addBookmark
)
}

I hoped that the addBookmark method would be invoked after the form
is submitted and that the returned SetHtml will set the new snippet
fragment (that should be processed by lift).
The new fragment is a new form:
<lift:Bookmark.edit>
<bookmark:url />
<bookmark:title />
<bookmark:submit />
</lift:Bookmark.edit>

Is there a better way to do such ajax fragment replacements? would
like to know more about.
Are the NodeSeq that should be replaced with SetHtml processed by
Lift?

The second thing is, that when I am trying to make a browser request
to view the page,
the addBookmark method is already executed. Was this intended?

with best regards

Alex Boisvert

unread,
Dec 29, 2009, 2:13:08 PM12/29/09
to lif...@googlegroups.com

I can't think of a better way right now;  what you're doing is pretty standard.
 
Are the NodeSeq that should be replaced with SetHtml processed by
Lift?

Not unless you're specifically calling templates, etc.
 
The second thing is, that when I am trying to make a browser request
to view the page,
the addBookmark method is already executed. Was this intended?

No, that's not supposed to happen.  I'm guessing you're calling addBookmark() somewhere else in your code.

alex

greekscala

unread,
Dec 29, 2009, 2:46:26 PM12/29/09
to Lift
Hello Alex,

thank you for the answer.

Ok I will check it again set up a little example.

Is this a "lift" way workflow?

- show a page with a form
- data is submitted to snippet
- data is evaluated
- get other fragments/templates and bind the values for them
- invoke SetHtml that should replace div content on the page

> Not unless you're specifically calling templates, etc.

Do I have to load my fragments with templateFinder and then
invoke other snippet methods that are intended to process the
templates xml or is there another way so that Lift can do it.

with best regards

On 29 Dez., 20:13, Alex Boisvert <alex.boisv...@gmail.com> wrote:


> On Tue, Dec 29, 2009 at 7:44 AM, greekscala <hellectro...@gmail.com> wrote:
> > Hello,
>
> > In a previous post I had the problem that my form was not send with
> > ajax.
> > With the help of Marius and Timothy I got it working.
> > (http://groups.google.com/group/liftweb/browse_thread/thread/

> > d052ed7191561ef1<http://groups.google.com/group/liftweb/browse_thread/thread/%0Ad052ed...>

greekscala

unread,
Dec 29, 2009, 3:59:33 PM12/29/09
to Lift
Hello again,

I think I found an error in the form processing, correcct me if it is
not:
This is the snippet:

class Test {
object name extends RequestVar[String]("init")

def render(xml: NodeSeq): NodeSeq = {
def printName(): JsCmd = {
Log.debug("name = " + name.is)

JsCmds.Alert("an ajax response! "+ name.is)
}
SHtml.ajaxForm(
Helpers.bind("atest", xml,
"name" -> SHtml.text(name.is, name(_)),
"submit" -> SHtml.submit("add", printName)
), JsCmds._Noop, printName
)

/*
SHtml.ajaxForm(
Helpers.bind("atest", xml,
"name" -> SHtml.text(name.is, name(_)),
"submit" -> SHtml.submit("add", printName)
) ++ SHtml.hidden(printName)
)
*/
}
}

This is the template:
<lift:surround with="default" at="col3">
<lift:Test>
<atest:name />
<atest:submit />
</lift:Test>
</lift:surround>

The problem is with the uncommented ajaxForm code, when the form is
submitted the name RequestVar is not updated. I seems that the
function
bound to the name-Textfield is not invoked. So I get back "an ajax
response! init".

But the commented ajaxForm code gives me a correnct result. If I type
"name" in the
textfield I get "an ajax response! name" back.

with best regards

Alex Boisvert

unread,
Dec 29, 2009, 4:03:21 PM12/29/09
to lif...@googlegroups.com
On Tue, Dec 29, 2009 at 11:46 AM, greekscala <hellec...@gmail.com> wrote:
Is this a "lift" way workflow?

- show a page with a form
- data is submitted to snippet
- data is evaluated
- get other fragments/templates and bind the values for them
- invoke SetHtml that should replace div content on the page

Yes, I'd say this is fairly a recurring use case.

> Not unless you're specifically calling templates, etc.

Do I have to load my fragments with templateFinder and then
invoke other snippet methods that are intended to process the
templates xml or is there another way so that Lift can do it.

Actually, I just tested it because I wasn't sure anymore and sure enough, Lift processes the content passed to SetHtml, so there's no need to call templateFinder unless you need to do more dynamic things. (I forgot about that, sorry about the wrong answer earlier)

alex

Marius

unread,
Dec 29, 2009, 4:28:54 PM12/29/09
to Lift
Could you please post your generated markup from the browser? ... Do
you see anything unusual in FireBug?

Br's,
Marius

greekscala

unread,
Dec 29, 2009, 4:58:00 PM12/29/09
to Lift
Hello Marius,

No there are no error messsages in the console window.

This is the generated form:
<form id="F587106754336BUG" action="javascript://"
onsubmit=";liftAjax.lift_ajaxHandler(jQuery
('#'+"F587106754336BUG").serialize(), function() { alert("an ajax
response! init");; }, null, "javascript");return false;"
postsubmit="alert("an ajax response! init");">
<input type="text" value="init" name="F5871067543340QE"/>
<input type="submit" name="F587106764335BDR" value="add"/>
</form>

Under the firebug network panel I see that there is a post ("x" is
typed value in the textfield):
F5871067543340QE x

And the response for this post is empty.

with best regards

I see that I get the predefined alert.

Alex Boisvert

unread,
Dec 29, 2009, 4:58:07 PM12/29/09
to lif...@googlegroups.com

It's invoked but it's invoked before the form field callbacks have been called so the field still holds the initial value.  (I've been wondering if this is the right behavior myself.)

The SHtml.hidden() callback is called after the form field callbacks so that's probably the one you want to use for now.

alex
Reply all
Reply to author
Forward
0 new messages