子对象的自定义确认对话框回调 [英] Custom confirmation dialog callbacks for sub-objects

查看:68
本文介绍了子对象的自定义确认对话框回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个自定义确认对话框,如果用户确认该操作,则该对话框可以接受回调和要执行的参数.

I have built a custom confirmation dialog that can take a callback and parameters to be executed if the user confirms the action.

function Confirm( text, callback, params )
{
    var html = '\
        <div class="modal-header clearfix">\
            <h1 class="title">Confirm</h1>\
        </div>\
        <div class="modal-content">\
            <p>' + ( text != null ? text : 'Are you sure you want to do this?' ) + '</p>\
        </div>\
        <div class="modal-footer clearfix">\
            <div class="float-right">\
                <div class="btn-group">\
                    <button class="btn ajax-modal-close">Cancel</button>\
                </div>\
                <div class="btn-group">\
                    <button class="btn important confirm">Ok</button>\
                </div>\
            </div>\
        </div>\
    ';

    Modal.dataModal(html, 640, 320); // Custom Modal framework

    $('.confirm').click(function(e){
        e.preventDefault();
        if(callback)
        {
            if(params)
            {
                callback = callback.replace(/window./gi, ''); // remove window. prefix so we can call the callback on the window object below
                if(Array.isArray(params))
                {
                    window[callback].apply(window, params);
                }
                else
                {
                    window[callback].call(window, params);
                }
            }
            else
            {
                window[callback]();
            }
        }
        Modal.closeModal( $('.Modal').last() ); // Custom Modal framework
    });
}

调用此函数的示例为:

Confirm('Are you sure? All your data will be lost!', 'window.location.replace', '../');

因此,如果用户确认此操作,它将把他们重定向到上一页.

So if the user confirms this action, it will redirect them to the previous page.

但是,这不适用于子对象,例如属于location而不是windowreplace.要调用它,我需要这样称呼:window['location']['replace'].call(..

However this doesn't work for sub-objects, such as replace which belongs to location and not window. To call this I would need to call it like: window['location']['replace'].call(..

如何处理子对象?当它们作为字符串传递时.

好像我直接在控制台宽度中调用它一样:window['location']['replace'].call(window, '../');它给出了错误:Illegal invocation.

As if I call it directly in the console width: window['location']['replace'].call(window, '../'); it gives the error: Illegal invocation.

推荐答案

要获得正确的功能和上下文,您可以执行以下操作:

To get the correct function and context you can do something like this:

var func_parts = callback.split("."),
    context = callback = window;

$.each( func_parts, function(index){
    if( this != "window" ) callback = callback[this];
    if( index == func_parts.length - 2 ) context = callback;
});

然后像这样使用它:

if (callback) {
    if (params) {
        if (Array.isArray(params)) {
            callback.apply(context, params);
        } else {
            callback.call(context, params);
        }
    } else {
        callback();
    }
}

这篇关于子对象的自定义确认对话框回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆