This was buried in the answer to another post but I think it will be
useful to some of you.

Components allow to insert a
{{=LOAD('controller','function',ajax=True)}} in a web2py view and the
actions /app/controller/function will manage its own content. It can
also communicate with other components and trigger events. For example
(complete program):

# models/db.py
db=DAL()
db.define_table('post',Field('body',notnull=True))
from gluon.tools import *
crud=Crud(globals(),db)

# controllers/default.py
def reload_posts(form):
response.js="web2py_ajax_page('get','%s',null,'posts')" %
URL('posts')
def index(): return dict()
def post(): return crud.create(db.post,onaccept=reload_posts)
def posts(): return SQLTABLE(db(db.post).select())

# views/index.html
{{extend 'layout.html'}}
{{=LOAD('default','post',ajax=True)}}
{{=LOAD('default','posts',ajax=True,target='posts')}}

post and posts are components embedded in the index page. submitting
an invalid post does not reload the page, only the component with its
errors and flash message. Submitting a valid post form, results in a
call (serverside) to reload_posts with sends a JS commend to the
client which forces an ajax reload of the list of previous posts.

Reply via email to