Public (static) property in jQuery plugin

I have a small jQuery plugin that I use to validate an AJAX form. There is a callback that warns of a script call of each "step" in the check / submit 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 set the stage variable of my plugin so that I can use it like this (which is easier to read than to use numbers)?

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

I assume that the notation ".formHelper.stage.Start" is not accurate, but I hope that I expressed my point of view.

+3
source share
1 answer

Your entries are in order:

(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);
+4
source

Source: https://habr.com/ru/post/1736449/


All Articles