开发者

How to pass parameters in Impromptu callback?

I'm using jQuery Impromptu as a pop window. The raw code is as follow:

function f1(pa1, pa2){
    $.prompt("need to开发者_StackOverflow中文版 delete?",{
         callback: callbackfunc,
         button: {OK: true, Cancel: false}
    });
}
function callbackfunc(v,m,f){
    if(v){
        // want to process the pa1 and pa2.
    }
}

How to process pa1 and pa2 inside the callback?


I use what you want in a project. See this:

function removePhoto(id){
    var txt = 'Are you sure you want to remove this photo?<input type="hidden" id="photoid" name="photoid" value="'+ id +'" />';

    $.prompt(txt,{ 
        buttons:{Delete:true, Cancel:false},
        callback: function(v,m,f){
            if(v){
                var pid = f.photoid;

                //Here is where you would do an ajax post to remove the user
                //also you might want to print out true/false from your .php
                //file and verify it has been removed before removing from the 
                //html.  if false dont remove, $promt() the error.
                $.get('<?php echo base_url(); ?>index.php/ajaxed/del_photo/'+pid, function(data){
                    if(data != 'false'){
                        $.prompt('The photo was successfuly deleted...', { callback: function() {
                            photosEdit--;
                            if(photosEdit == 0) {
                                window.location = "<?php echo base_url(); ?>index.php<?php echo ($row_p->aid == 0 ? "/gallery/view/me" : "albums/view/".$this->dx_auth->get_user_id()."/".$row_p->aid); ?>";
                            } else {
                                $("div#editWrapper_"+pid).hide("slow", function() { $(this).remove();});
                            }
                        } });
                        //recirect history-1
                    }else{ $.prompt('An error occured while removing this photo...'); }                         
                });
            }
            else{}
        }
    });
}


I solve my problem like this:

function f1(pa1, pa2){
   $.prompt("need to delete?",{
       callback: function(v, m, f){
            if(v){
               //  process of pa1, pa2...
            }
       },
       button: {OK: true, Cancel: false}
   });
}

It's OK if there is less parameters to process. however, if the callback function is too complex, and there are much parameters, this method is a not good.

I hope somebody could give me a better suggestion.


If your code on the callback method is getting complex, the best way to do this is:

var g_pa1,g_pa2;  // General variables

function f1(pa1, pa2){

    g_pa1 = pa1;
    g_pa2 = pa2;

    $.prompt("need to delete?",{
     callback: callbackfunc,
     button: {OK: true, Cancel: false}
    });
}

function callbackfunc(v,m,f){
    if(v){
        //PROCESS YOUR g_pa1 and g_pa2
        g_pa1 = null;
        g_pa2 = null;
    }
}


I'm not sure if the plugin changed this or there was a wrong documentation somewhere, but the callback is submit nowadays and it takes 4 arguments instead of 3: event, value, message, formVals:

buttons: { Cancel: false, Next: true },
submit:function(e,v,m,f){ }

Adapting @Oytun Tez excelent answer, I've made this example:

<html>
<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="impromptu.js"></script>
    <link rel="stylesheet" media="all" type="text/css" href="impromptu.css" />

    <script type="text/javascript">
    function removePhoto( id )
    {
        var txt = 'Are you sure you want to remove this photo?<input type="hidden" id="photoid" name="photoid" value="'+ id +'" />';
        $.prompt( txt,
        { 
            title: "Remove Photo",
            buttons:{ Delete:true, Cancel:false },
            submit: function( event, value, message, formVals )
            {
                if( value )
                {
                    var pid = formVals.photoid;
                    console.log('pid: ' + pid );
                }
            }
        });
    }

    $(document).ready( function() 
    {
        $('.btn').on('click', function( e )
        {
            removePhoto( $(this).data('id') ); 
        });
    });
    </script>
</head>
<body>
    <input class="btn" type="button" value="Remove photo 1" data-id="1" />
    <input class="btn" type="button" value="Remove photo 2" data-id="2" />
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜