Skip to main content
added 113 characters in body
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37

I suggest looking at KernelJS, which implements this pattern

I suggest looking at KernelJS, which implements this pattern

added 742 characters in body
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37

SampleHere's a simple, yet incomplete implementation using this new approach (it lacks the document.ready part). It is long, to be placedyet it's modular and easily maintainable. You can even split the section codes in your base code:separate files.

Module(function (ns) {

  ns.addSection = function (name, def) {
 
  //check if namens[name] and= new def();
 are what}

}(this.Module they= are:this.Module string|| and{}));

//adding functionconfig
Module.addSection('Config', function () {
  //createvar ansystemName instance= of'mySystem',
 the constructor  version = '1.1.4',
  var  section = newthis;

 def section.getSystemInfo = function ();
 {
  //add the sectionreturn tosystemName the+ module' namespace
v' + Module[name]version;
 = section;}

});

Here's a simple usage for such functionality with Config:


//adding Dialog
Module.addSection('Config''Dialog', function () {

  //everythingvar insection here= isthis;

 private section.onDocumentReady = function () {
    $('.showDialog').on('click', function () {
      var systemNameobj = 'mySystem'{
        element: this, 
 version = '1.1.4'     foo: 'foo',
        bar: 'bar',
        target: $('#title')
      };
      section.setParam(obj);
  //the namespace for this section.show();
 is provided via "this"})
 so you}

 can exposesection.setParam = function (config) {
  //your public interfaceconsole.log('config set for thisdialog sectionbox');
  this  console.getSystemInfolog(config);
  }

  section.show = function () {
    returnconsole.log('dialogbox systemNameshow +| ' v'+version;+ Module.Config.getSystemInfo());
  }

  //yousection.close: mayfunction want() to{
 add special functions thatconsole.log('dialogbox getclose');
 fired on}

});

//Savescreen certainsection
Module.addSection('Savescreen', eventsfunction () {

  //likevar forsection example,= firethis;

 upon addsection.setParam or= firefunction when(config, document.readyfoo) {
  //implementation depends onconsole.log('config youset for savescreen');
  this}

  section.onDocumentReadyshow = function () {
    console.log('this'savescreen getsshow fired| on' document+ ready'Module.Config.getSystemInfo());
  }
 
});

Sample implementation, to be placed in your base code:

Module.addSection = function(name,def){
 
  //check if name and def are what they are: string and function
  
  //create an instance of the constructor
  var section = new def();

  //add the section to the module namespace
  Module[name] = section;
}

Here's a simple usage for such functionality with Config:

Module.addSection('Config',function(){

  //everything in here is private
  var systemName = 'mySystem'
    , version = '1.1.4'
    ;

  //the namespace for this section is provided via "this" so you can expose
  //your public interface for this section
  this.getSystemInfo = function(){
    return systemName + ' v'+version;
  }

  //you may want to add special functions that get fired on certain events
  //like for example, fire upon add or fire when document.ready
  //implementation depends on you
  this.onDocumentReady = function(){
    console.log('this gets fired on document ready');
  }
 
});

Here's a simple, yet incomplete implementation using this new approach (it lacks the document.ready part). It is long, yet it's modular and easily maintainable. You can even split the section codes in separate files.

(function (ns) {

  ns.addSection = function (name, def) {
    ns[name] = new def();
  }

}(this.Module = this.Module || {}));

//adding config
Module.addSection('Config', function () {
  var systemName = 'mySystem',
    version = '1.1.4',
    section = this;

  section.getSystemInfo = function () {
    return systemName + ' v' + version;
  }

});

//adding Dialog
Module.addSection('Dialog', function () {

  var section = this;

  section.onDocumentReady = function () {
    $('.showDialog').on('click', function () {
      var obj = {
        element: this, 
        foo: 'foo',
        bar: 'bar',
        target: $('#title')
      };
      section.setParam(obj);
      section.show();
    })
  }

  section.setParam = function (config) {
    console.log('config set for dialog box');
    console.log(config);
  }

  section.show = function () {
    console.log('dialogbox show | ' + Module.Config.getSystemInfo());
  }

  section.close: function () {
    console.log('dialogbox close');
  }

});

//Savescreen section
Module.addSection('Savescreen', function () {

  var section = this;

  section.setParam = function (config, foo) {
    console.log('config set for savescreen');
  }

  section.show = function () {
    console.log('savescreen show | ' + Module.Config.getSystemInfo());
  }

});
added 266 characters in body
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37
Module.addSection = function(name,def){

  //check if name and def are what they are: string and function
  
  //thencreate wean addinstance of the definitionconstructor
  //basically, def is a constructorvar andsection you= arenew interfacingdef();

  //with an instance ofadd itthe thatsection isto attachedthe tomodule Modulenamespace
  Module[name] = new def();
section;
}
Module.addSection('Config',function(){

  //everything in here is private
  var systemName = 'mySystem'
    , version = '1.1.4'
    ;

  //the namespace for this section is provided via "this" so you can expose
  //your public interface for this section
  this.getSystemInfo = function(){
    return systemName + ' v'+version;
  }

  //you may want to add special functions that get fired on certain events
  //like for example, fire upon add or fire when document.ready
  //implementation depends on you
  this.onDocumentReady = function(){
    console.log('this gets fired on document ready');
  }
 
});
Module.addSection = function(name,def){

  //check if name and def are what they are: string and function
  
  //then we add the definition
  //basically, def is a constructor and you are interfacing
  //with an instance of it that is attached to Module
  Module[name] = new def();

}
Module.addSection('Config',function(){

  //everything in here is private
  var systemName = 'mySystem'
    , version = '1.1.4'
    ;

  //the namespace for this section is provided via "this" so you can expose
  //your public interface for this section
  this.getSystemInfo = function(){
    return systemName + ' v'+version;
  }
 
});
Module.addSection = function(name,def){

  //check if name and def are what they are: string and function
  
  //create an instance of the constructor
  var section = new def();

  //add the section to the module namespace
  Module[name] = section;
}
Module.addSection('Config',function(){

  //everything in here is private
  var systemName = 'mySystem'
    , version = '1.1.4'
    ;

  //the namespace for this section is provided via "this" so you can expose
  //your public interface for this section
  this.getSystemInfo = function(){
    return systemName + ' v'+version;
  }

  //you may want to add special functions that get fired on certain events
  //like for example, fire upon add or fire when document.ready
  //implementation depends on you
  this.onDocumentReady = function(){
    console.log('this gets fired on document ready');
  }
 
});
added 417 characters in body
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37
Loading
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37
Loading