Hi, I have a component which has a button that performs an action when 
clicked but I want the user accept some terms before executing that action.
I found Sweet Alert (http://t4t5.github.io/sweetalert/) so I am trying to 
use their confirm dialog between the click and the action. Since Sweet 
Alert is entirely javascript (and css) I am calling my action using the 
*ajax* method passing the parameters the controller gives to the view. 

The problem I am facing is that the action receives wrong values in 
request.vars. Every time I click the action button I receive a different 
parameter. Why is that?

This is my component controller:

def vuelo_ofertante():
    response.delimiters = ('<?', '?>')
    vuelo_id = request.vars['vid']
    pedido_id = request.vars['pid']
    vuelo = db(db.viajes.id == vuelo_id).select(db.viajes.ALL).first()
    lugar_vuelo = db(db.lugares.id == 
vuelo.lugar).select(db.lugares.nombre, db.lugares.lugar_padre).first()

    if lugar_vuelo is None:
        texto_lugar = ''
    else:
        lugar_padre_vuelo = db(db.lugares.id == 
lugar_vuelo.lugar_padre).select(db.lugares.nombre).first()
        if lugar_padre_vuelo is not None:
            texto_lugar = lugar_vuelo.nombre + ' (' + 
lugar_padre_vuelo.nombre + ')'
        else:
            texto_lugar = lugar_vuelo.nombre

    preorden_vuelo = db((db.preordenes.viaje == vuelo.id) & 
(db.preordenes.pedido == pedido_id)).select(db.preordenes.ALL).first()

    return dict(vuelo=vuelo, lugar_vuelo=texto_lugar, 
*preorden_vuelo=preorden_vuelo*) (1)

def pedir():
    id_viaje = request.vars['vid']
    *id_pedido = request.vars['pid'] #*I always get a random value here 
where I expect to get* the preorden_vuelo.pedido *(1) value that I am 
returning in the vuelo_ofertante method

and this is my component's .load view:

<div class="well well-lg<?if preorden_vuelo.preorden_aprobada:?> 
well-green<?pass?>">
    <?if preorden_vuelo.preorden_aprobada:?>
        <span class="glyphicon glyphicon-ok"></span>
        <b><?=T('Gracias!')?></b>
    <?pass?>
    <b><?=T('Fecha de retorno: ')?></b><?=vuelo.fecha_retorno?>
    <br>
    <b><?=T('Lugar: ')?></b>
    <?if vuelo.lugar is not None:?>
        <?=lugar_vuelo?>
    <?else:?>
        <?=T('No especificado')?>
    <?pass?>
    <br>
    <?if preorden_vuelo.preorden_aprobada is False:?>
        <br>
        <?=T('Costo: ')?><b>$<?=preorden_vuelo.costo_cliente?></b>
        <br>
        <br>
        <a class='btn btn-primary' 
onclick='confirmacion();'><?=T('PEDIR')?></a> <!-- does not work -->
        <!-- <?=A(T('PEDIR'), _class='btn btn-primary', 
callback=URL('confirmar_pedido', 'pedir', vars={'vid':vuelo.id, 
'pid':preorden_vuelo.pedido}))?> --> <!-- Does work as expected but I don't 
know how to use Sweet Alert here -->
    <?pass?>
    <script>
        function swal_aceptado(){
            swal({
                    title: "<?=T('Muchas gracias!')?>",
                    text: "<h4><?=T('Nos comunicaremos con usted.')?></h4>",
                    html: true,
                    type: "success",
                    allowEscapeKey: false,
                    confirmButtonText: "<?=T('Entendido')?>"
                 }, function(){
                    eval("ajax('<?=URL('confirmar_pedido', 
'redireccionar')?>', [], target=null)");
                 });
        }

        function confirmacion(){
            swal({
                    title: "<?=T('Esta a punto de llevar a cabo un 
acuerdo')?>",
                    text: '<h4><?=T('Asegurese que a leído y aceptado 
nuestros ')?><?=A(T('términos y condiciones'), _href=URL('default', 
'eula'))?></h4>',
                    html: true,
                    type: "info",
                    showCancelButton: true,
                    allowOutsideClick: true,
                    confirmButtonText: "<?=T('Estoy de acuerdo!')?>",
                    cancelButtonText: "<?=T('Cancelar')?>",
                    closeOnConfirm: false,
                    showLoaderOnConfirm: true
                }, function() {
                    setTimeout(function(){
                        <?print vuelo.id, preorden_vuelo.pedido?>
                        ajax('<?=URL('confirmar_pedido', 'pedir', 
vars={'vid':vuelo.id, *'pid':preorden_vuelo.pedido*})?>', [], target=null);
                        //eval("ajax('<?=URL('confirmar_pedido', 'pedir', 
vars={'vid':vuelo.id, 'peid':preorden_vuelo.pedido})?>', [], target=null)");
                        swal_aceptado();
                    }, 500);
                });
            }
    </script>
</div>

Any help will be greatly appreciated. Thank you guys!

-- 
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