Last active
October 27, 2015 19:36
-
-
Save shwaydogg/2d6efe39d90601836f1a to your computer and use it in GitHub Desktop.
Global Meteor Monkey Calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Meteor.call = (function (original) { | |
return function (name) { | |
var cb; | |
console.log('Meteor Method ' + name + ' called!' ); | |
//Check for/find original CallBack: | |
if(_.isFunction(arguments[1])){ | |
cb = 1; | |
}else if(_.isFunction(arguments[2])){ | |
cb = 2; | |
}else if(_.isFunction(arguments[3])){ | |
cb = 3; | |
}else{ | |
cb = arguments.length; | |
arguments[cb] = (function(){}); | |
arguments.length++; | |
} | |
if(cb){ | |
arguments[cb] = (function (original){ | |
return function(error, result){ | |
if(error){ | |
Materialize.toast(error.message, 4000, 'red darken-2'); | |
if(error.details){ | |
_.forEach(error.details, function(detail){ | |
Materialize.toast(detail, 10000, 'red darken-2'); | |
}); | |
} | |
} | |
if(!error){ | |
Materialize.toast(name + ' Success!', 2000, 'green darken-2'); | |
} | |
if(result){ | |
Materialize.toast(result, 4000, 'green darken-2'); | |
} | |
return original.apply(this, arguments); | |
} | |
//Original Callback: | |
})(arguments[cb]); | |
} | |
// execute | |
return original.apply(this, arguments); | |
} | |
})(Meteor.call); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment