On 20 feb, 11:38, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Yes there are some trick in here. Everything happens in one line
>
Thanks. Tricky, yes! very!. It is hard to follow at first but I am
getting it now.

> db.person.organization.comment = ajax_create(db.person.organization)
>
> the function ajax_create serves a double purpose:
> 1) it returns a helper instance that when you click on it it open a
> modal window that contains a LOAD component. LOAD components in web2py
> are loaded via ajax and all form submissions are trapped so they also
> submitted via ajax too and the response stays into the modal window.
> But where is the ajax callback function?
> 2) it acts as a callback function! How can it be? well, in web2py you
> can raise HTTP. This means any piece of code, not just action can
> return HTTP responses. If the ajax_create function detects a request
> vars called _ajax_add set to a a value equal to the name of the field
> passed as argument, it understand the client is doing the ajax
> callback and it raise HTTP thus producing a response without
> continuing execution of models and controllers. In this case it acts
> as an action (even if defined in a model).
>
>     if request.get_vars._ajax_add==str(field):
>         ... # this is acts as an action
>         raise
> HTTP(200,crud.create(table,onaccept=update_select).xml(),...)
>
Searching the group I found only two posts using it and obviously I
did not understand the capabilities of it before.

> When the form is accepted it also sets a header variable called
> 'web2py-component-command'. This is executed by the ajax form trap
> client-side. The command tells the client to replace the content of
> the SELECT with the new one and close the modal.
>
This must be part of "not documented here" in AlterEgo 252.
Is it documented now?

> I agree it is not easy but you do not really need to understand it to
> use it.
>
I prefer to understand.

> Massimo
>
> On Feb 20, 9:37 am, DenesL <denes1...@yahoo.ca> wrote:
>
> > Aw crap! and here is little me thinking I know how to use web2py.
> > This one requires some painfully detailed walk-through.
> > Am I the only one?.
>
> > On 20 feb, 02:39, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > En even better solution.
>
> > > Drop this code in your model somewhere:
>
> > > def ajax_create(field,
> > >                 value='create',
> > >                 title='Add a new organizaiton',
> > >                 height=100, width=600):
> > >     if not field.type.startswith('reference'):
> > >         raise SyntaxError, "can only be used with a reference field"
> > >     if not hasattr(field.requires,'options'):
> > >         raise SyntaxError, "cannot determine options from field
> > > validator"
> > >     key = str(field).replace('.','_')
> > >     if request.get_vars._ajax_add==str(field):
> > >         def update_select(form):
> > >             options = TAG['']
> > > (*[OPTION(v,_value=k,_selected=str(form.vars.id)==str(k)) \
> > >                                     for (k,v) in
> > > field.requires.options()])
> > >             command = "jQuery('#
> > > %s').html('%s');jQuery('#TB_closeWindowButton').click()" \
> > >                 % (key,options.xml().replace("'","\'"))
> > >             response.headers['web2py-component-command'] = command
> > >         table = field._db[field.type[10:]]
> > >         raise
> > > HTTP(200,crud.create(table,onaccept=update_select).xml(),**response.headers)
> > >     response.files.append('http://jquery.com/demo/thickbox/thickbox-
> > > code/thickbox.js')
> > >     response.files.append('http://jquery.com/demo/thickbox/thickbox-
> > > code/thickbox.css')
> > >     return TAG[''](
> > >         A(value,_class='thickbox',_title=title,
> > >           _href='#TB_inline?height=%s&width=%s&inlineId=TB_%s' %
> > > (height,width,key)),
> > >         DIV(LOAD(request.controller,request.action,args=request.args,
> > >                  vars=dict(_ajax_add=field),ajax=True),_id='TB_%s' %
> > > key,_class='hidden'))
>
> > > Then you just do:
>
> > > db.define_table('organization',Field('name',notnull=True,unique=True),format='%
> > > (name)s')
> > > db.define_table('person',Field('name'),Field('organization',db.organization))
>
> > > db.person.organization.comment = ajax_create(db.person.organization,
> > > title='Add an Org.')
>
> > > def index():
> > >     return dict(form=crud.create(db.person,request.args(0)))
>
> > > Will work with any table/field that you already have.

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to