简体   繁体   English

将 function 作为回调传递给 function

[英]pass function as a callback to function

I have this code:我有这个代码:

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument(window.lastSelectedRowId), 'Delete document');                    
}); 

function showConfirmDialog(content, callback, pTitle, obj){
    return showDialogEx(content, callback, pTitle, obj, "Yes", "No");
}

function showDialogEx(content, callback, pTitle, obj, okButtonLabel, cancelButtonLabel){
    var str = "#_showMessageDialog";
    var showMessageDialog = $(str);
    if(showMessageDialog.length == 0){
        $('body').append('<div id="_showMessageDialog"></div>');
        showMessageDialog = $(str);
    }
    showMessageDialog.val("");
    showMessageDialog.append('<p id="_showMessageDialogContent">'.concat(content, '</p>'));

    var my_buttons = {};
    my_buttons[cancelButtonLabel] = function(){
        $(this).dialog("close");
        $(this).html("");
        $(this).dialog("destroy");
    };
    my_buttons[okButtonLabel] = function(){
        callback();
        $(this).html("");
        $(this).dialog("close");
        if(obj){
            obj.focus();
        }
        $(this).dialog("destroy");
    };

    showMessageDialog.dialog({
        modal : true,
        resizable : true,
        title : pTitle,
        minWidth : 250,
        width : 450,
        buttons : my_buttons
    });

}

And then I click on button with classes .b-icon.b-icon_del it seems like executing both deleteDocument(window.lastSelectedRowId) and showConfirmDialog('Are you sure you want to delete this document?',deleteDocument(window.lastSelectedRowId), 'Delete document');然后我单击带有类.b-icon.b-icon_del按钮,它似乎同时执行deleteDocument(window.lastSelectedRowId)showConfirmDialog('Are you sure you want to delete this document?',deleteDocument(window.lastSelectedRowId), 'Delete document'); at the moment.眼下。 I just want that callback function (which is deleteDocument(window.lastSelectedRowId) ) invoke after user clicks "OK" button.我只想在用户单击“确定”按钮后调用该回调 function (即deleteDocument(window.lastSelectedRowId) )。 Thanks!谢谢!

You are executing the function deleteDocument(window.lastSelectedRowId) when you pass it as the second argument showConfirmDialog .当您将 function deleteDocument(window.lastSelectedRowId)作为第二个参数showConfirmDialog传递时,您正在执行它。

You should just pass it, as well as the argument window.lastSelectedRowId , along to showConfirmDialog without executing it.您应该只将它以及参数window.lastSelectedRowIdshowConfirmDialog而不执行它。

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument, window.lastSelectedRowId, 'Delete document');                    
}); 

function showConfirmDialog(content, callback, rowId, pTitle, obj){
    return showDialogEx(content, callback, rowId, pTitle, obj, "Yes", "No");
}

function showDialogEx(content, callback, rowId, pTitle, obj, okButtonLabel, cancelButtonLabel){
    var str = "#_showMessageDialog";
    var showMessageDialog = $(str);
    if(showMessageDialog.length == 0){
        $('body').append('<div id="_showMessageDialog"></div>');
        showMessageDialog = $(str);
    }
    showMessageDialog.val("");
    showMessageDialog.append('<p id="_showMessageDialogContent">'.concat(content, '</p>'));

    var my_buttons = {};
    my_buttons[cancelButtonLabel] = function(){
        $(this).dialog("close");
        $(this).html("");
        $(this).dialog("destroy");
    };
    my_buttons[okButtonLabel] = function(){
        callback(rowId);
        $(this).html("");
        $(this).dialog("close");
        if(obj){
            obj.focus();
        }
        $(this).dialog("destroy");
    };

    showMessageDialog.dialog({
        modal : true,
        resizable : true,
        title : pTitle,
        minWidth : 250,
        width : 450,
        buttons : my_buttons
    });

}
$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument, [window.lastSelectedRowId], 'Delete document');                    
}); 

function showConfirmDialog(content, callback, callbackArguments, pTitle, obj){
    return showDialogEx(content, callbackArguments,  pTitle, obj, "Yes", "No");
}

and in:并在:

function showDialogEx(content, callback, callbackArguments, pTitle, obj, okButtonLabel, cancelButtonLabel)

the lines:行:

my_buttons[okButtonLabel] = function(){
    callback();

Should be:应该:

my_buttons[okButtonLabel] = function(){
    callback.apply(this, callbackArguments);

It is important to note that callbackArguments is a list ["value'] even if it only has one value.需要注意的是,callbackArguments 是一个列表["value'] ,即使它只有一个值。

Alternatively, wrap deleteDocument(window.lastSelectedRowId) in a function to prevent it from being evaluated prematurely at the call to $('.b-icon.b-icon_del').click() .或者,将deleteDocument(window.lastSelectedRowId)包装在 function 中,以防止在调用$('.b-icon.b-icon_del').click()时对其进行过早评估。

ie IE

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?', function() { return deleteDocument(window.lastSelectedRowId); }                  
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM