Skip to content

Instantly share code, notes, and snippets.

@mmhan
Forked from anonymous/script.js
Created October 3, 2011 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmhan/1258821 to your computer and use it in GitHub Desktop.
Save mmhan/1258821 to your computer and use it in GitHub Desktop.
Basic Structure For JS
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Lorem ipsum</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write("<script src='js/libs/jquery-1.6.2.min.js'>\x3C/script>")</script>
<script src="script.js"></script>
</body>
</body>
</html>
myApp = myApp or {}
myApp = ->
privateProperty = "This is a private property"
privateMethod = ->
console.log "This is a private method."
# Magic Init function.
__init__ = ->
@beforeAutoInit() if @beforeAutoInit and $.isFunction(@beforeAutoInit)
for i of this
#if it actually is a module and init is a function.
if $.isPlainObject(this[i]) and $.isFunction(this[i].init)
#if autoInit is not defined.
if this[i].autoInit is `undefined`
this[i].init()
else
#init if autoInit is defined (as function and returns true) or (as boolean and return true)
this[i].init() if ($.isFunction(this[i].autoInit) and this[i].autoInit()) or ($.type(this[i].autoInit) is "boolean" and this[i].autoInit)
@afterAutoInit() if @afterAutoInit and $.isFunction(@afterAutoInit)
obj =
init: __init__
publicMethod: ->
console.log "This is a public method."
beforeAutoInit: ->
console.log "beforeAutoInit"
afterAutoInit: ->
console.log "afterAutoInit"
Module: {}
ModuleWillAutoInit:
init: ->
console.log "ModuleWillAutoInit : automatically initiated"
ModuleInitBasedOnPredefinedCondition:
autoInit: false
init: ->
console.log "ModuleInitBasedOnPredefinedCondition: this will not run."
ModuleInitBasedOnResultOfAFunction:
autoInit: ->
Math.floor Math.random() * 2
init: ->
console.log "ModuleInitBasedOnResultOfAFunction: this will run randomly"
obj
$(document).ready ->
myApp.init()
var myApp = myApp || {};
myApp = function () {
var privateProperty = 'This is a private property';
var privateMethod = function () {
console.log('This is a private method.');
};
/**
* MagicInit function
**/
var __init__ = function () {
if(this.beforeAutoInit && $.isFunction(this.beforeAutoInit)) this.beforeAutoInit();
for(var i in this){
//if i is a module and init fn exists
if($.isPlainObject(this[i]) && $.isFunction(this[i].init)){
if(
//init will run autoInit property is not defined
this[i].autoInit == undefined ||
(
//if autoInit is defined
this[i].autoInit != undefined &&
(
//if autoInit is a function execute it and see what it says.
($.isFunction(this[i].autoInit) && this[i].autoInit()) ||
//if autoInit is a boolean property, check if it is true or not.
($.type(this[i].autoInit) === 'boolean' && this[i].autoInit)
)
)
){
this[i].init();
}
}
}
if(this.afterAutoInit && $.isFunction(this.afterAutoInit)) this.afterAutoInit();
};
var obj = {
//redefine this as a normal function if MagicInit is not going to be used.
init: __init__,
/**
* Method documentation
**/
publicMethod: function () {
console.log('This is a public method.');
},
/**
* stuffs to be executed, before MagicInit is used.
* remove this function if MagicInit is not used.
**/
beforeAutoInit: function(){
console.log("beforeAutoInit");
},
/**
* stuffs to be executed after MagicInit is used.
* remove this function if MagicInit is not used.
**/
afterAutoInit: function(){
console.log("afterAutoInit");
},
/*======================*
* Module documentation
* This module doesn't have init() hence will not get initialized
*======================*/
Module: {},
/*======================*
* This module will init automatically
*======================*/
ModuleWillAutoInit: {
init: function(){
console.log('ModuleWillAutoInit : automatically initiated');
}
},
/*======================*
* This module will not init automatically
*======================*/
ModuleInitBasedOnPredefinedCondition:{
autoInit: false,
init: function(){
console.log('ModuleInitBasedOnPredefinedCondition: this will not run.');
}
},
/*======================*
* This module will init randomly
*======================*/
ModuleInitBasedOnResultOfAFunction:{
autoInit: function(){
//some voodoo magic should be here.
return Math.floor(Math.random() * 2);
},
init: function(){
console.log("ModuleInitBasedOnResultOfAFunction: this will run randomly");
}
}
};
return obj;
}();
$(document).ready(function () {
myApp.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment