Anchor callback option and form submit

207 views
Skip to first unread message

Riccardo C

unread,
Nov 3, 2013, 3:06:02 PM11/3/13
to web...@googlegroups.com
Good evening,

I've just discovered the possibility to use in the view of the callback option when we want to create a link, e.g. {{=A('mypage', callback(URL='myfunction'), target='myID'). I found it really useful for not loading a new page again. The problem is that the callback calls a function that return a simple form that is correctly shown inside that div but when i press the 'submit' button... the page refresh, but nothing happen on the db. Basically nothing is register.

What am I doing wrong? I am not really interested in fixing this specific problem rather to understand how it works and what I didn't get.

Regards,

Riccardo

P.S. I attach the code if it might help:
def addPet():
    db
.pet.owner_id.readable = False
    db
.pet.owner_id.writable = False
    db
.pet.owner_id.default = auth.user.id
    form
= crud.create(db.pet, onaccept=lambda form: testAnotherFunction(form.vars.id), next=URL('welcome'), message='The pet has been added')
   
return dict(form = form)

IN THE VIEW WELCOME
.HTML
...
<li>{{=A('Add a Pet', callback=URL('addPet'), target="MainContent" )}} </li>
...
<div id="MainContent">
    Replace the content
</
div>


ADDAPET
.HTML
<div class="row">
       
<div  class="span5 offset1"id="addPetForm" >
           
{{=form}}
       
</div>
</
div>



Niphlod

unread,
Nov 3, 2013, 5:18:49 PM11/3/13
to web...@googlegroups.com
the callback argument is not meant to load a fragment and keep it like a component (meaning, trapping all links, form submissions, redirections etc. in that fragment).
callback was meant instead to either click on a button and show the result in a fragment (i.e. "action completed") and/or click a button to remove an element from the page (i.e. remove a line from a table).

For a "component-like" behaviour, please use
A('mypage', _href=URL('myfunction'), cid='myID')
or the equivalent
A('mypage', _href=URL('myfunction'), target='myID')

Anthony

unread,
Nov 3, 2013, 8:09:39 PM11/3/13
to web...@googlegroups.com

A('mypage', _href=URL('myfunction'), target='myID')

Shouldn't above be:

A('mypage', component=URL('myfunction'), target='myID')


Riccardo Ceccarelli

unread,
Nov 4, 2013, 2:47:34 AM11/4/13
to web...@googlegroups.com
Thank you very much, I'll try it later.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/A1_6kz-SBg0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
Riccardo Ceccarelli

Niphlod

unread,
Nov 4, 2013, 3:58:51 PM11/4/13
to web...@googlegroups.com

does the exact same thing but I don't think that component as an argument was ever documented.

Anthony

unread,
Nov 4, 2013, 5:28:25 PM11/4/13
to web...@googlegroups.com

A('mypage', _href=URL('myfunction'), target='myID')

Shouldn't above be:

A('mypage', component=URL('myfunction'), target='myID')



does the exact same thing but I don't think that component as an argument was ever documented.

I just meant that according to the code, the data-w2p_method attribute is set only in case "component", "callback", or "cid" are provided, so specifying "target" without one of those other three arguments won't result in any Ajax behavior, no?

Anthony
 

Niphlod

unread,
Nov 5, 2013, 3:03:33 PM11/5/13
to web...@googlegroups.com
nope, https://github.com/web2py/web2py/blob/master/gluon/html.py#L1506 adds it for cid.

Basically when I reviewed all the combinations for the A helper porting it to work with the new data-style attributes, I found no use for the component argument because it was really similar to something achievable without it (and had no bonus points since it was never documented). It was kept for backward compatibility and nothing else.

For all intents and purposes, the two syntaxes (component and target "vs" href and cid) generate the same markup (hence they do the same thing).

Anthony

unread,
Nov 5, 2013, 3:38:15 PM11/5/13
to web...@googlegroups.com
Not sure we're talking about the same thing. I'm just saying that if you specify only the "target" attribute, but not "cid" or "component", then you don't get an Ajax component:

>>> A('mypage', _href=URL('myfunction'), target='myID').xml()
'<a data-w2p_disable_with="default" data-w2p_target="myID" href="/test/default/myfunction">mypage</a>'

Notice in the above output, there is no data-w2p_method attribute, which is required in order to get the Ajax behavior. So, in the above, you either have to replace "_href" with "component" or replace "target" with "cid".

Anthony

Niphlod

unread,
Nov 5, 2013, 4:56:16 PM11/5/13
to web...@googlegroups.com
ok, let me rephrase the final statement...

The "official" way (since _href and cid are way back in web2py's history) should be

A('mypage', _href=URL('myfunction'), cid='myID')

Full Stop(s).
.
.
.

For backward compatibility, the following does the same thing, but I don't think it's documented (still supported, but we may deprecate its usage)


A('mypage', component=URL('myfunction'), target='myID')

@all: sorry for the original typo
Reply all
Reply to author
Forward
0 new messages