Search code examples
javascriptjqueryjsonmodal-dialogfunction-calls

javascript window['func'] with parameters returning undefined instead of function


I'm trying to pass json to a function, which in turn takes the json and displays dynamic ui-modal dialog. When I try to pass the button events(functions) , thy are getting executed and returning undefined how can I pass reference to the function instead of actual function. Sorry for my english the code below explains much better:

//heres my ui model function
function showDialog(json) {
        var dialog_id = json.id;
        if(typeof($(dialog_id)[0]) === "undefined") {
            $(body).append("<div id='dialog-confirm'></div>");
        }
        $(dialog_id).html(json.confirmationText);
        $(dialog_id).dialog({
            modal: true,
            title: json.title,
            height: 250,
            width: 400,
            buttons:json.buttons
        });

}

//here is the function which calls
showDialog({"id":"#dialog-confirm","title":"blaa","confirmationText":'randomtext',"buttons":[{"text":"Cancel","click":window["noAction"]},{"text":"Confirm","click":window["doAction"]('pram1','param2')}]});

var noAction = function(){$('#dialog-confirm').dialog('close');}
var doAction = function(param1,param2){//do some logic};

noAction function is working correctly. (when the model is called and cancel is clicked the dialog disappears)

doAction function is getting evaluated when the function showDialog is called and is returning undefined (verified in console). How can I bind doAction to confirm button. (ie, when Confirm is clicked I want to call doAction).

P.S: I know how to do it with custom on click handlers but I think it would be a work around but not the actual solution.

Any help is greatly appreciated. Thanks in advance!


Solution

  • try showDialog({..., "buttons":[...,{"text":"Confirm","click":function(){doAction(param1,param2);} }]})