views:

36

answers:

1

I've a small jQuery plugin that I use for form AJAX validation. There is a callback that alerts the calling script of every "stage" in the validation/submission process.

Some snippets:

(function ($) {
    $.fn.formHelper = function (options) {
        // settings options etc
        var stage = { Start: 1, ErrorReceived: 2, AllErrorsReceived: 3, NoErrors: 4 };
        // rest of the plugin
    };
})(jQuery);

$("#some_button").formHelper({
    StageCallback: someCallbackHandler;
});

function someCallbackHandler(stage) {
    switch(stage) {
        case 1: alert("Starting validation"); break;
        case 2: alert("Error received"); break;
        case 3: alert("All errors received"); break;
        case 4: alert("No errors"); break;
    }
}

Question: How can I expose the stage variable of my plugin so I can use it similar like this (which is easier to read than using numbers)?

function someCallbackHandler(stage) {
    if (stage == $.formHelper.stage.Start)
        alert("Starting validation");
}

I suppose the "$.formHelper.stage.Start" notation isn't exact, but I hope I made my point with it.

+2  A: 

Your notation is fine:

(function ($) {
    $.formHelper = {
      stage: { Start: 1, ErrorReceived: 2, AllErrorsReceived: 3, NoErrors: 4 }
    };
    $.fn.formHelper = function (options) {
        // settings options etc
        var stage = $.formHelper.stage;
        // rest of the plugin
    };
})(jQuery);
gnarf
Thanks! You saved me some hours of searching... :o)
Niels R.