I use Module Pattern in my Javascript application, and I want to migrate to RequireJS and AMD.
My current implementation is like this :
// Module main : Module.js
var myModule = (function(my, $, undefined) {
'use strict';
var m_privateMembers;
var m_stuff = new my.Pear({color:"green"});
//---------------------------------------
function privateFunctions(args) {
}
//-----------------------------------
my.publicFunctions = function(args) {
};
return my;
})(myModule || {}, jQuery);
// Module class Fruit : Module.Fruit.js
var myModule = (function(my, $, undefined) {
'use strict';
my.Fruit = Object.makeSubclass();
my.Fruit.prototype._init = function() {
};
my.Fruit.prototype.someClassFunction = function() {
};
return my;
})(myModule || {}, jQuery);
// Module class Pear : Module.Pear.js
var myModule = (function(my, $, undefined) {
'use strict';
my.Pear = Fruit.makeSubclass();
my.Pear.prototype._init = function(args) {
this.m_someClassMember = args;
};
my.Pear.prototype.someClassFunction = function() {
};
return my;
})(myModule || {}, jQuery);
In my index.html I use script html tags to include my different modules files and everything is working fine.
My question are :
1/ Why RequireJS system is better than this multiple script tags system ?
With my current RequireJS approach :
define(['jquery'], function($) {
var myModule = (function(my, $, undefined) {
'use strict';
var m_privateMembers;
var m_stuff = new my.Pear({color:"green"});
//---------------------------------------
function privateFunctions(args) {
}
//-----------------------------------
my.publicFunctions = function(args) {
};
return my;
})(myModule || {}, $);
return myModule;
});
2/ How could I migrate to RequireJS modules and keep the singleton-like pattern that my actual modules provide with embedded sub classes ?
obviously there is several problems :
2.1/ *m_stuff can not instantiate my.Pear, and I don't know how to split modules in several files with require ?*
2.2/ If I have two other modules that both are dependent on myModule, will the myModule var be the same instance in those module (since myModule is not in the global scope anymore) ?
edit: this quick schema illustrate what I need to do, in a much more complex framework :