0

I am trying to create javascript extendable library with submodules in different files. I am using Module Pattern based on Ben Cherry article

module.js

var SERVICES = (function (service) {

  var service = {},
    privateVariable = 1;

  function privateMethod() {
    //
  }

  service.moduleMethod = function () {
    //
  };

  return service;

}());

submodule.js

SERVICES.submodule = (function (service) {

  var submodule = {},
    privateVariable = 1;

  submodule.moduleMethod = function () {
    //
  };

  return submodule;

}(SERVICES));

What I want to achieve is the following. The module.js is a module which I want to act as a library. Submodules are separated in the different files, thus I am using Module Pattern. Clients will be able to write new submodules for the mentioned library (module.js). I want to create a script which will call library SERVICES with all submodules available. This script will be included in one front-end file just once, and there will be no need to change anything when someone writes new submodule. Library should load it.

dzeno
  • 510
  • 1
  • 5
  • 16
  • What do you mean by "*with the new script*"? The module pattern abstraction is completely unaware of *how* the modules will be loaded - from simple script inclusion to a fullblown dependency management framework. So what are you using, what are you talking about when you say "*add it to load list*" or "*register new submodules*"? – Bergi Jan 30 '14 at 02:20
  • Note that in `var SERVICES = (function (service) {var service = {};}(SERVICES))`, passing in *SERVICES* is a waste of time. It probably hasn't been assigned a value and even it it has, it isn't used and is immediately replaced. – RobG Jan 30 '14 at 03:00
  • In your submodule.js script file the function expects a variable called submodule (the second parameter) and later you declare a local variable called submodule and init it to a empty object. Then, why to pass the second parameter to the function if it isn't used at all? – Marco Muciño Jan 31 '14 at 04:05
  • @Pispirulito I removed code, because it is not relevant for the question. I updated question. Thank you. – dzeno Feb 04 '14 at 23:04
  • @Bergi Thanks for answer, I was unclear with my question. I updated it now. – dzeno Feb 04 '14 at 23:11
  • You mean you want to automatically load all submodules in a page without explicitly including each of them? – Bergi Feb 04 '14 at 23:37
  • @Bergi Yes and that way avoid including new submodule when is added – dzeno Feb 05 '14 at 09:02
  • You always will need to notify your code of new submodules. Whether you just append it to the large library file, or register it in a complex module dependency system doesn't make much difference. There are indeed systems that allow you to do only one edit for a new submodule, that will lead to its inclusion in all pages, but this question is way too broad. – Bergi Feb 05 '14 at 13:24

0 Answers0