Great advice Sam. That is definitely one mistake I made in my last
application. I was exploring the module pattern and I created several
modules for different sections of my page. I started calling methods
on other modules and soon enough it became hard to maintain and test.

Say you have 2 modules on a page.  Would you recommend setting each
module up using the module pattern, returning a method to initialize
the module, and passing each module a global PubSub object which can
be used to publish and listen for events? Something like this:

var PubSub = {
   publish: function ( ) { },
   subscribe: function ( ) { },
   unsubscribe: function ( ) { }
};


var module1 = (function () {

   return {
      init: function () { } //used to initialize the module on the
page
   }
}) ( PubSub );



var module2 = (function () {

   return {
      init: function () { } //used to initialize the module on the
page
   }
}) ( PubSub );

On Jul 19, 1:57 am, Sam Foster <[email protected]> wrote:
> A simple, useful rule of thumb when considering architecture, is to
> never have a object communicate directly with something it didn't
> itself create. I.e. calling methods on objects that are assumed to
> have been created also - bad thing. That's how I break down when to
> call methods directly vs. when to use pub/sub. The thing that created
> me should hand me a list of pub/sub channel names, or a prefix which I
> can use to push out and listen for events. If 2 things need to be
> connected, the connection is shaped by the thing that makes them.
>
> Following this simple rule reveals a lot of of hidden assumptions
> which can later turn into bugs, and it makes for more testable code.
> It also makes clear when you need some kind of a manager or controller
> to sit above components and dispatch events, and hold the shared state
> for the collection of objects it manages.
>
> /Sam

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to