Hey,

I wanted to change web2py response.flash to use bootstrap3 alerts.

I started by creating an alert-default in my css since bootstrap3 doesn't 
have one and I wanted it, then I placed this in my layout.html


<div class="{{='flash alert alert-dismissable ' + ('alert-' + 
(response.flash_status or 'default'))}}">{{if response.flash:}}
                  {{=response.flash}}
                  <button type="button" class="close" 
data-dismiss="alert"><span aria-hidden="true">&times;</span><span 
class="sr-only">Close</span></button>
                  {{pass}}</div>

response.flash_status was my solution so the controllers were able to 
determine what kind of flash they would want the view to show, this was 
working quite well until I started having stuff working via ajax. If a 
request was done using ajax all my alerts were being shown with default. 
After a bit of digging I found that  $.web2py.flash also had a "status" 
argument that could add a class so I thought this would be quite simple. 
Nothing could be further from the truth. In the end I had to do this:

1. Add this to my 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)


2. Add a js file to the project which I add after all the other js files 
containing this:

$.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">&times;</span><span class="sr-only">' +
        w2p_ajax_close_message +
        '</span>' +
        '</button>'
      ).slideDown();
    }
};


Anyway, my question is, am I doing something wrong here? Wasn't there a 
simpler way to implement this?






-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to