7

I am trying to provide a call back function for an ajax call, where the function name is saved under the "data-apply" attribute of a form.

jQuery(function($) {
    $('form[data-async]').on('submit', function(event) {
        var $form = $(this);
        var $target = $($form.attr('data-target'));
        var apply = $form.attr('data-apply');
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),

            success: function(data, status) {
                var callBackFunc = new Function(apply);
                callBackFunc.call();
            }
        });

        event.preventDefault();
    });
});

function addBubble(){
    alert('a');
}

The form:

<form id="111" class="form-horizontal " data-async data-target="#rating-modal" data-apply="addBubble();"  action="/some/action/performed" method="POST">

I want to avoid using eval() in this case, since eval() might introduce security and performance issues. Not sure Function() is a safer method or there is a completely different way to handle this.

So is there anyway to pass in a function callback in order to handle difference situations?

The original code was borrowed from: https://gist.github.com/havvg/3226804

2 Answers 2

12

Since the function is globally defined, you can call it through the window object.

window["functionName"].call();

Remove the parentheses from addBubble

<form id="111" class="form-horizontal " data-async data-target="#rating-modal" data-apply="addBubble"  action="/some/action/performed" method="POST">

Javascript

jQuery(function($) {
    $('form[data-async]').on('submit', function(event) {
        var $form = $(this);
        var $target = $($form.attr('data-target'));
        var apply = $form.attr('data-apply');
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),

            success: function(data, status) {
                if(typeof window[apply] == "function")
                    window[apply].call(); //window[apply](); or window[apply].apply(); will work too
            }
        });

        event.preventDefault();
    });
});

function addBubble(){
    alert('a');
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can just do window[apply](). The .call() is used if you want to change the this value inside the function.
I know, I wanted to respect the way he was calling the callback function, that's all
5

If the function is defined globally (i.e. on the window object) you could use window[$form.attr('data-apply')]() to call the callback. Note that you would have to remove the () in data-apply

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.