Ok here it goes, brace yourself.
First in layout.html replace the original flash div with:
<div class="{{='flash alert alert-dismissable ' + ('alert-' +
(response.flash_status or 'default'))}}">{{if
response.flash:}}{{=response.flash}}{{pass}}</div>
This will be enough to put some color in it using response.flash_status in
a controller for example:
response.flash = 'Form Accepted'
response.flash_status = 'success'
However what about session.flash and session.flash_status? You can add this
to your models:
if session.flash_status:
response.flash_status = session.flash_status
del session.flash_status
Ok now we have it working with session and response, however what about
when we get a flash from an ajax action that has a response.flash (for
instance a form in a loaded component). This one is slightly more
complicated and you need to add this to your models:
def ajax_flash_status(view):
import urllib2
if response.flash:
if not response.flash_status:
response.flash_status = 'default'
response.headers['web2py-component-flash-status'] = \
urllib2.quote('alert-' + xmlescape(response.flash_status)\
.replace('\n',''))
return view
if request.ajax:
response.postprocessing.append(ajax_flash_status)
This is not enough, the only thing we did is make sure the status goes in a
response header if we're answering an ajax request. Now we need to use it.
To do that, we need to "patch" web2py.js, so you need to create a
flashstatus.js file in your static folder and make sure it's included in
your layout.html *AFTER* web2py.js. There, you put the following code:
$.web2py.after_ajax = function (xhr) {
/* called whenever an ajax request completes */
var command = xhr.getResponseHeader('web2py-component-command');
var flash = xhr.getResponseHeader('web2py-component-flash');
var flash_status = xhr.getResponseHeader('web2py-component-flash-status'
);
if(command !== null) {
eval(decodeURIComponent(command));
}
if(flash) {
$.web2py.flash(decodeURIComponent(flash), decodeURIComponent(
flash_status));
}
};
$.web2py.flash = function (message, status) {
var flash = $('.flash');
$.web2py.hide_flash();
flash.html(message);
if (typeof status !== 'undefined') {
flash.removeClass('alert-default alert-success alert-info
alert-warning alert-danger').addClass(status);
}
if(flash.html()) {
flash.append(
'<button type="button" class="close" data-dismiss="alert">' +
'<span aria-hidden="true">×</span><span class="sr-only">' +
w2p_ajax_close_message +
'</span>' +
'</button>'
).slideDown();
}
};
Aaaand you're done!
I should probably make a plugin out of this to simplify stuff.
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.