Created
July 24, 2012 20:07
-
-
Save dgs700/3172293 to your computer and use it in GitHub Desktop.
Pattern for importing/exporting a simple Javascript Module / Singleton from Ext.define()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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