0

I'm coding a jquery plugin that act as a loading link that sends a request to a server using GET or POST now the thing is really basic but I cannot figure it out, maybe I'm tired, hehehehe.

Here is my code

Plugin instantiation:

$('#load-products-button').loadingLink({
    params: function(){
                return {                        
                   provider_id: $('#provider_id').val()
                }
    },
    method: 'post',
    success: function(res){
         alert('ok');
    }
});

And the code that makes the call is:

var params = settings.params ? settings.params : null;

$[settings.method](url, params)
     .success(function() {
       settings.success.apply(this, arguments);
     })
     .fail(function() {
       settings.error.apply(this, arguments);
     })
     .complete(function() {
       //remove the loading html
       link.html(currentHtml);
     });

The thing is that when sending the post request the params are not sent because them contain a function call, the post request is delivered with no parameters.

If I replace the params with an object like {provider_id: $('#provider_id').val()} it works correctly but sending params on plugin instantiation is not working to me.

How to eval the params before sending?

Thanks in advance.

1
  • 1
    Well, since settings.params is a function, call it as one. var params = settings.params ? settings.params() : null; Commented Jun 9, 2017 at 19:47

1 Answer 1

1

Since params is a function, you will have to call it to get the values. In you plugin, change your code to the following:

var params = null;
if (settings.params) {
    if (typeof settings.params == "function") {
        params = settings.params();
    }
    else {
        params = settings.params;
    }
}
Sign up to request clarification or add additional context in comments.

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.