Can I use the Javascript Module template for singletons, as well as for objects that are created several times?

I have one page with two types of forms. I have one type A form at the top, and then I have 1 or more type B forms below.

I use the module template + jQuery to connect all events in my forms, validate, ajax calls, etc.

Is this the preferred / acceptable way to define a singleton, as in form A, and a class of reusable objects, as in form B? They are very similar, and I'm not sure if I need to use an object prototype, newor another template. Everything seems to work for me, but I'm afraid I'm missing a key error.

Form A javascript looks like this:

var MyProject.FormA = (function() {
  var $_userEntry;
  var $_domElementId;

  var validate = function() { 
    if($_userEntry == 0) {
      alert('Cannot enter 0!');
    } 
  }
  var processUserInput = function() {
    $_userEntry = jQuery('inputfield', $_domElementId).val();
    validate();
  }
  return {
    initialize: function(domElementId) {
      $_domElementId = domElementId;
      jQuery($_domElementId).click(function() {
        processUserInput();
      }
    }
  }

})();
jQuery(document).ready(function() {
  MyProject.FormA.initialize('#form-a');
});

Form B, , :

var MyProject.FormB = function() {
  var $_userEntry;
  var $_domElement;

  var validate = function() { 
    if($_userEntry == 0) {
      alert('Cannot enter 0!');
    } 
  }
  var processUserInput = function() {
    $_userEntry = jQuery('inputfield', $_domElement).val();
    validate();
  }
  return {
    initialize: function(domElement) {
      $_domElement = domElement;
      jQuery($_domElement).click(function() {
        processUserInput();
      }
    }
  }

};
jQuery(document).ready(function() {
  jQuery(".form-b").each(function() {
    MyProject.FormB().initialize(this);
  });
});
+3
2

, .

, . , , , .

, ( , ) , ( , , )

, , , . , , . A, ? , ? , . , non-singleton (B) :

var getSingleton = function() {
    var form = MyProject.FormB();
    form.initialize("#form-a");
    console.log("This is the original function");
    getSingleton = function() {     
        console.log("this is the replacement function");
        return form;
    }
    return form;
}
+2

, jQ:

(function($) {
    $.fn.formValidator = function() {
        return $(this).each(function() {
            var $_domElement = $(this);
            $_domElement.click(function() {
                if($('inputfield', $_domElement).val() == 0) {
                    alert('Cannot enter 0!');
                }
            });
        });
    };
})(jQuery);

jQ ( ). .

:

$('#form-a').formValidator();
$('.form-b').formValidator();

$('#form-a, .form-b').formValidator();

, :

ProjectModule.formValidator = function(selector) {
    return $(selector).each(function() {
        var $_domElement = $(this);
        $_domElement.click(function() {
            if ($('inputfield', $_domElement).val() == 0) {
                alert('Cannot enter 0!');
            }
        });
    });
};

:

ProjectModule.formValidator('#form-a, .form-b');
+1

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


All Articles