4

I'm trying to grasp the concept of pluggable modules, like jQuery does with plugins. My goal is to split my modules into different files and initiliaze them properly after the DOM is ready according to jQuery.

Example

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="my_library.js"></script>
    <script type="text/javascript" src="modules.js"></script>
    <!-- Could be more -->

The way I am doing it right now is by calling MY_LIBRARY.init() inside a jQuery function after I set my modules.

// my_library.js
var MY_LIBRARY = {
    init: function() {
        var _self = this,
            modules = _self.modules;

        if (modules) {
            for (var i in modules){
                modules[i].init();
            }
        }
    },
    modules: {}
}

// modules.js
MY_LIBRARY.modules.MyModule1 = {

    init: function(){
        // initialize stuff
        return this;
    }

};

MY_LIBRARY.modules.MyModule2 = {

    init: function(){
        // initialize stuff
        return this;
    }

};

$(function(){
    MY_LIBRARY.init();
});

The question is how can I self-invoke MY_LIBRARY and the modules when the DIM is ready, without using:

$(function(){
    MY_LIBRARY.init();
});

Update

I've been working on this, trying to replicate the way jQuery does this, so I've written some code that uses 'X$' instead of '$' as my custom global object which allows me to do something like:

X$( 'MyModule1', { init: function(){}, doStuff: function(){} });

or

X$('MyModule1').doStuff();

I think this pattern works for me. At the moment, the only thing I couldn't replicate is using something like:

X$.core.myCoreFunction() { return true; }; // plugin function yay!

X$.myCoreFunction(); // alerts X$.myCoreFunction is not a function

Which is kind of weird since jQuery does this with plugins. Any thoughts on this one?

2
  • What's wrong with including one of those DOM ready blocks at the end of each file? Commented Feb 3, 2011 at 13:31
  • @Box9 You are right, but i was wondering if we have a more sophisticated pattern for doing this. Commented Feb 3, 2011 at 14:13

1 Answer 1

2

I'd try to use a function wrapper around your module definitions, especially since you may find that there are a lot of commonalities between your modules, besides the init function.

For instance, here I define a lib function which helps you declare a new library, and will automatically bind up the init function to document.ready:

function lib(module) {
  $(function() {
    if (module.init) {
      module.init();
    }
  });

  return module;
}

var MYLIB = lib(function() {
   return {
     init: function() {
       // ...
     }
   };
}());

You could use another variation on the same pattern, but it will keep you from needing to add a lot of extra code, and allow you to manage all your libraries in the same ways. You could also, for instance, have lib return a dummy a object when it's called initially, and after document.ready it applies the function passed in to generate the real object, which would allow you to avoid an explicit init entirely:

function lib(moduleMaker) {
  var module = {};
  $(function() {
    moduleMaker(module);
  });
  return module;
}

var MYLIB = lib(function(mylib) {
   mylib.foo = function() {/*...*/};
   // other init stuff, we've already waited for document.ready
});
Sign up to request clarification or add additional context in comments.

1 Comment

I 've updated my code. It seems like the pattern used in jQuery solved my first problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.