Reputation: 500
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.
Upvotes: 0
Views: 31
Reputation: 234
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;
}
}
Upvotes: 1