Skip to content

Instantly share code, notes, and snippets.

@dgs700
Created July 24, 2012 20:07
Show Gist options
  • Save dgs700/3172293 to your computer and use it in GitHub Desktop.
Save dgs700/3172293 to your computer and use it in GitHub Desktop.
Pattern for importing/exporting a simple Javascript Module / Singleton from Ext.define()
//define a dependancy
Ext.define( 'My.dependancyModule', {
//prevents unnecessary class construction methods
singleton: true,
//wrap logic scoped in self executing function
module: (function(){
//protected vars
var myDependancy = 'dependancy';
//return public members
return {
getMyDependancy: function(){
return myDependancy;
}
};
})()
}, function(){
//use Ext.define callback to reset the object to just the module
My.dependancyModule = this.module;
});
//gets the benefits of Ext loading while removing unnecessary Ext class members
Ext.define( 'My.exampleModule', {
//load another simple module as a dependancy
requires: ['My.otherModule'],
//prevents unnecessary class construction methods
singleton: true,
//wrap logic scoped in self executing function
module: (function(){
//protected vars
var myVar1 = 'my ',
myVar2 = '';
//return public members
return {
//public vars, functions, accessor methods
myVar3: 'three',
getMyVar1: function(){
return myVar1;
},
getMyVar2: function(){
return myVar2;
},
//access our dependancy
writeSomething: function(){
myVar2 = My.otherModule.getMyDependancy();
console.log( myVar1 + myVar2);
}
};
})()
}, function(){
//use Ext.define callback to reset the object to just the module
My.exampleModule = this.module;
});
My.exampleModule.writeSomething(); // 'my depndancy'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment