web2py_ajax.html

The scaffolding web2py application “welcome” includes a file called

  1. views/web2py_ajax.html

which looks like this:

  1. {{
  2. response.files.insert(0, URL('static', 'js/jquery.js'))
  3. response.files.insert(1, URL('static', 'css/calenadar.css'))
  4. response.files.insert(2, URL('static', 'js/calendar.js'))
  5. response.include_meta()
  6. response.include_files()
  7. }}
  8. <script type="text/javascript"><!--
  9. // These variables are used by the web2py_ajax_init
  10. // function in web2py.js (which is loaded below).
  11. var w2p_ajax_confirm_message =
  12. "{{=T('Are you sure you want to delete this object?')}}";
  13. var w2p_ajax_date_format = "{{=T('%Y-%m-%d')}}";
  14. var w2p_ajax_datetime_format = "{{=T('%Y-%m-%d %H:%M:%S')}}";
  15. //--></script>
  16. <script src="{{=URL('static', 'js/web2py.js')}}"
  17. type="text/javascript"></script>

This file is included in the HEAD of the default “layout.html” and it provides the following services:

  • Includes “static/jquery.js”.
  • Includes “static/calendar.js” and “static/calendar.css”, which are used for the popup calendar.
  • Includes all response.meta headers
  • Includes all response.files (requires CSS and JS, as declared in the code)
  • Sets form variables and includes “static/js/web2y.js”

“web2py.js” does the following:

  • Defines an ajax function (based on jQuery $.ajax).
  • Makes any DIV of class “error” or any tag object of class “flash” slide down.
  • Prevents typing invalid integers in INPUT fields of class “integer”.
  • Prevents typing invalid floats in INPUT fields of class “double”.
  • Connects INPUT fields of type “date” with a popup date picker.
  • Connects INPUT fields of type “datetime” with a popup datetime picker.
  • Connects INPUT fields of type “time” with a popup time picker.
  • Defines web2py_ajax_component, a very important tool that will be described in Chapter 12.
  • Defines web2py_websocket, a function that can be used for HTML5 websockets (not described in this book but read the examples in the source of “gluon/contrib/websocket__messaging.py”).
  • Defines functions to the entropy calculation and input validation of the password field.

It also includes popup, collapse, and fade functions for backward compatibility.

Here is an example of how the other effects play well together.

Consider a test app with the following model:

  1. db = DAL("sqlite://storage.sqlite")
  2. db.define_table('child',
  3. Field('name'),
  4. Field('weight', 'double'),
  5. Field('birth_date', 'date'),
  6. Field('time_of_birth', 'time'))
  7. db.child.name.requires=IS_NOT_EMPTY()
  8. db.child.weight.requires=IS_FLOAT_IN_RANGE(0, 100)
  9. db.child.birth_date.requires=IS_DATE()
  10. db.child.time_of_birth.requires=IS_TIME()

with this “default.py” controller:

  1. def index():
  2. form = SQLFORM(db.child)
  3. if form.process().accepted:
  4. response.flash = 'record inserted'
  5. return dict(form=form)

and the following “default/index.html” view:

  1. {{extend 'layout.html'}}
  2. {{=form}}

The “index” action generates the following form:

image

If an invalid form is submitted, the server returns the page with a modified form containing error messages. The error messages are DIVs of class “error”, and because of the above web2py.js code, the errors appears with a slide-down effect:

image

The color of the errors is given in the CSS code in “layout.html”.

The web2py.js code prevents you from typing an invalid value in the input field. This is done before and in addition to, not as a substitute for, the server-side validation.

The web2py.js code displays a date picker when you enter an INPUT field of class “date”, and it displays a datetime picker when you enter an INPUT field of class “datetime”. Here is an example:

image

The web2py.js code also displays the following time picker when you try to edit an INPUT field of class “time”:

image

Upon submission, the controller action sets the response flash to the message “record inserted”. The default layout renders this message in a DIV with id=”flash”. The web2py.js code is responsible for making this DIV appear and making it disappear when you click on it:

image

These and other effects are accessible programmatically in the views and via helpers in controllers.