Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add ajax form support
  • Loading branch information
rnixx committed May 4, 2012
1 parent e73646c commit c813732
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
40 changes: 40 additions & 0 deletions README.rst
Expand Up @@ -461,6 +461,40 @@ attribute ``ajaxtarget`` on the received event a convenience is provided.::
bdajax.trigger('contextchanged', '.contextsensitiv', url)


AJAX Forms
----------

To process ajax forms, a hidden iframe is used where the form gets triggered to.
The form must be marked with CSS class ``ajax`` in order to be handled by
bdajax. The server side must return a response like so::

<div id="ajaxform">
<form class="ajax"
method="post"
action="http://example.com/myformaction"
enctype="multipart/form-data">
...
</form>
</div>
<script language="javascript" type="text/javascript">
var container = document.getElementById('ajaxform');
var child = container.firstChild;
while(child != null && child.nodeType == 3) {
child = child.nextSibling;
}
parent.bdajax.render_ajax_form(child, '#form_selector', 'fiddle_mode');
parent.bdajax.continuation({});
</script>

If ``div`` with id ``ajaxform`` contains markup, it gets rendered to
``#form_selector`` with ``fiddle_mode``. This makes it possible to rerender
forms on validation error or display a success page or similar. Further
bdajax continuation definitions can be given to ``parent.bdajax.continuation``.

Again, bdajax does not provide any server side implementation, it's up to you
providing this.


3rd Party Javascript
--------------------

Expand Down Expand Up @@ -500,6 +534,12 @@ Contributors
Changes
=======

1.4dev
------

- Add AJAX form support.
[rnix, 2012-05-04]

1.3
---

Expand Down
2 changes: 1 addition & 1 deletion setup.py
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import sys, os

version = '1.3'
version = '1.4dev'
shortdesc = 'Ajax convenience.'
longdesc = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
longdesc += open(os.path.join(os.path.dirname(__file__), 'LICENSE.rst')).read()
Expand Down
8 changes: 7 additions & 1 deletion src/bdajax/bdajax.pt
Expand Up @@ -18,4 +18,10 @@
<button class="submit allowMultiSubmit">OK</button>
<button class="cancel allowMultiSubmit">Cancel</button>
</div>
</div>
</div>

<iframe id="ajaxformresponse"
name="ajaxformresponse"
src="#"
style="width:0px;height:0px;display:none">
</iframe>
32 changes: 29 additions & 3 deletions src/bdajax/resources/bdajax.js
@@ -1,5 +1,5 @@
/*
* bdajax v1.3
* bdajax v1.4
*
* Requires:
* - jQuery 1.6.4
Expand Down Expand Up @@ -33,6 +33,9 @@
}
}
});
// XXX: probably ajax forms get a separate ``ajax:form`` directive
// in markup.
bdajax.bind_ajax_form(context);
for (var binder in bdajax.binders) {
bdajax.binders[binder](context);
}
Expand All @@ -45,7 +48,7 @@
// That we assume at '/login'.
default_403: '/login',

// object for 3rd party binders
// object for hooking up JS binding functions after ajax calls
binders: {},

// ajax spinner handling
Expand Down Expand Up @@ -239,7 +242,11 @@
},

overlay: function(options) {
var elem = $('#ajax-overlay');
var selector = '#ajax-overlay';
if (options.selector) {
selector = options.selector;
}
var elem = $(selector);
elem.removeData('overlay');
var url, params;
if (options.target) {
Expand Down Expand Up @@ -355,6 +362,25 @@
elem.data('overlay').load();
},

// bind ajax form handling to all forms providing ajax css class
bind_ajax_form: function(context) {
var ajaxform = $('form.ajax', context);
ajaxform.append('<input type="hidden" name="ajax" value="1" />');
ajaxform.attr('target', 'ajaxformresponse');
ajaxform.unbind().bind('submit', function(event) {
bdajax.spinner.show();
});
},

// called by iframe response, renders form (i.e. if validation errors)
render_ajax_form: function(payload, selector, mode) {
if (!payload) {
return;
}
this.spinner.hide();
this.fiddle(payload, selector, mode);
},

_dispatching_handler: function(event) {
event.preventDefault();
event.stopPropagation();
Expand Down

0 comments on commit c813732

Please sign in to comment.