Skip to content

Instantly share code, notes, and snippets.

@IOZ
Created March 14, 2014 09:07
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 IOZ/9544440 to your computer and use it in GitHub Desktop.
Save IOZ/9544440 to your computer and use it in GitHub Desktop.
Pattern Module
/**
* Pattern Module
* p - parent object
* t - reference to this into the module
*/
var App;
App = (function(){
return {};
})();
/**
* Module Helper
*/
App.Helper = (function(p){
return {
getViewPort: function(side){
var side = side || 'both';
var viewport = { width: 0, height: 0 };
if (typeof window.innerWidth != 'undefined') {
viewport.width = window.innerWidth;
viewport.height = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewport.width = document.documentElement.clientWidth;
viewport.height = document.documentElement.clientHeight;
}
// older versions of IE
else {
viewport.width = document.getElementsByTagName('body')[0].clientWidth;
viewport.height = document.getElementsByTagName('body')[0].clientHeight;
}
switch(side){
case 'width':
return viewport.width;
break;
case 'height':
return viewport.height;
break;
case 'both':
return viewport;
break;
}
}
}
})(App);
/**
* Module Responsive
*/
App.Responsive = (function(p){
// private reference to this
var t;
return {
init: function(){
t = this;
t.checkBreakPoints(p.Helper.getViewPort('width'));
t.resize();
},
checkBreakPoints: function(w){
if(w < 768) {
t.devices.mobile();
} else if ( w >= 768 && w <= 1024 ) {
t.devices.tablet();
} else if ( w > 1024 ) {
t.devices.desktop();
}
},
resize: function(){
jQuery(window).on('resize', function(){
t.checkBreakPoints(p.Helper.getViewPort('width'));
});
},
devices : {
mobile: function(){
console.log('mobile devices');
},
tablet: function(){
console.log('tablet');
},
desktop: function(){
console.log('desktop');
}
}
}
})(App);
$(function(){
App.Responsive.init();
});
/**
* Page: Cart
*/
var App = (function(p){
return p;
})(App || {});
App.Cart = (function(p){
var t;
return {
init: function(){
t = this;
console.log('do some with cart');
console.log(t); // current instance
console.log(p); // parent instance
}
}
})(App);
jQuery(function(){
App.Cart.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment