I´m running several method calls that need to show the end user a loading modal and hide it when the method resturns a result. I was looking for a way to run this pre-call code and post-call code for every method without repeating code.
swal({
title: "Saving...",
onBeforeOpen: () => swal.showLoading()
});
Meteor.call("method", {/*params*/}, (err, res) => {
//Do something
swal.hide();
});
I want to be able to run those 2 swal codes without writing that code in each call. Is there some way to configure Meteor.call to do something before and after the call to the method?
You could abstract the code into a wrapper function that accepts your method name, parameters and callback function as arguments:
Note here, that
callbackis not the "real" callback but just put inside the statements and receives the arguments from the "real" callback itself as arguments.Use the method for example like so:
If you need this function a lot you can put it in an own file and use
exportto make it available to other files/modules.