javascript - Pass function to submitHandler callback -
i use mainsubmithandler
multiple pages, , willing define global variable if necessary. mainsubmithandler
, however, requires tweaking, , handling using subsubmithandler
. instead of having subsubmithandler
global variable, how can pass agrument mainsubmithandler
?
var mainsubmithandler=function(form) { //do bunch of stuff subsubmithandler(form); }; var subsubmithandler=function(form) { //do stuff }; // uses jquery validation plugin var validator=$("#form1").validate({ rules: {}, messages: {}, submithandler: mainsubmithandler });
you can use bind
here.
bind
wraps around function reference allowing pass scope , variables targeted function:
function.bind(thisarg[, arg1[, arg2[, ...]]])
parameters:
- thisarg: value passed parameter target function when bound function called. value ignored if bound function constructed using new operator.
- arg1, arg2, ...arguments prepend arguments provided bound function when invoking target function.
source mdn
var mainsubmithandler=function(form, callback) { //do bunch of stuff if (typeof(callback) != "undefined" && object.prototype.tostring.call(callback) === "[object function]") //sanity check. check if callback function , exists. { callback(form); } }; var subsubmithandler=function(form) { //do stuff }; // uses jquery validation plugin var validator=$("#form1").validate({ rules: {}, messages: {}, submithandler: mainsubmithandler.bind(null, form, subsubmithandler); //first argument set null. passes argument of targeted function. });
Comments
Post a Comment