Last active
December 20, 2015 05:38
-
-
Save tjoskar/6079425 to your computer and use it in GitHub Desktop.
Common javascript structure for small JS project
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
UTIL = { | |
fire: function( controller, action, args ) { | |
var namespace = NAME; | |
action = ( action === undefined ) ? "init" : action; | |
if ( controller !== '' && namespace[controller] && typeof namespace[controller][action] == "function" ) { | |
V.log('Fire: ' + controller + '.' + action + '()'); | |
namespace[controller][action](args); | |
} | |
}, | |
init: function() { | |
var body = document.body, | |
controller = body.getAttribute( "data-controller" ), | |
action = body.getAttribute( "data-action" ); | |
UTIL.fire( "common" ); | |
UTIL.fire( controller ); | |
if (action !== '') | |
UTIL.fire( controller, action ); | |
UTIL.fire( "common", "finalize" ); | |
} | |
}; | |
// Kick it all off | |
$(document).ready(UTIL.init); |
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
var NAME = { | |
log: function(msg) { | |
console.log(msg); | |
}, | |
error: function(msg) { | |
console.error(msg); | |
}, | |
time: function() { | |
return new Date().getTime(); | |
}, | |
isset: function(obj) { | |
return obj !== undefined; | |
}, | |
isArray: function(variable) { | |
return variable instanceof Array; | |
}, | |
compile: function(template_name, context) { | |
var source = $("#" + template_name).html(); | |
var template = Handlebars.compile(source); | |
return template(context); | |
}, | |
save: function(key, data) { | |
if (localStorage) { | |
localStorage[key] = JSON.stringify({ | |
'time': NAME.time(), | |
'data': data | |
}); | |
} | |
}, | |
get: function(key) { | |
if (localStorage) { | |
var t = localStorage[key]; | |
if (!NAME.isset(t)) | |
return null; | |
try { | |
t = JSON.parse(t); | |
} catch(err) { | |
localStorage.removeItem(key); | |
return null; | |
} | |
if (t['time'] < NAME.time() - 86400000) | |
return null; | |
NAME.save(key, t['data']); | |
return t['data']; | |
} else { | |
return null; | |
} | |
}, | |
cunvertToSlug: function(text) { | |
return text.toLowerCase().replace(/-+/g, '').replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, ''); | |
}, | |
post: function(action_type, post_data, success) { | |
$.post(NAME.url.api + action_type + '/', post_data, function(data) { | |
success(data); | |
}, 'json').fail(function() { alert('AJAX error'); }); | |
}, | |
alert: function(msg, type, duration) { | |
duration = duration || 5000; | |
type = type || 'info'; | |
var a = $('<div></div>').addClass(type).text(msg); | |
$('#alert_holder').append(a); | |
a.click(function() { | |
a.hide(); | |
}); | |
if (duration > 0) { | |
setTimeout(function() { | |
a.fadeOut(1000); | |
}, duration); | |
} | |
}, | |
lock: function(force) { | |
if (force || !NANME.variable.lock) { | |
NAME.variable.lock = true; | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
unlock: function() { | |
NAME.variable.lock = false; | |
}, | |
url: { | |
'base': null, | |
'api': null, | |
'update': null | |
}, | |
common: { | |
init: function() { | |
// Guess the current url | |
if (NAME.url.base === null) { | |
NAME.url.base = window.location.origin; // webkit only !DONT USE! | |
NAME.url.api = NAME.url.base + 'api/'; | |
} | |
other_stuff(); | |
}, | |
finalize : function() { | |
One_last_thing(); | |
} | |
}, | |
controller1: { | |
init : null, | |
action : null | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment