Skip to content

Instantly share code, notes, and snippets.

@liamr
Created May 20, 2013 10:13
Show Gist options
  • Save liamr/5611434 to your computer and use it in GitHub Desktop.
Save liamr/5611434 to your computer and use it in GitHub Desktop.
App starting point, using the garber/irish execution + require.js. Also, a bunch of data-behavior code
/*
PROJECT
LiamR
Usage:
<body data-controller="users" data-action="show">
<div data-behavior="init_something">
*/
// --------------------------------------------------------------------------------------------------------------
//CONFIGURE REQUIRE js
require.config({
paths : {
'jquery': 'vendor/jquery-1.7.1.min',
//'scrollto': 'libs/scrollto'
},
priority : [
'jquery' //execute jquery before any other dependency
]
})
//APP
APP = {
initial_run : true,
common: {
init: function() {
// application-wide code
//Look for Behaviors set up and trigger looking for the behaviors on DOM ready
APP.LoadBehavior();
//Run AJAXIFY once
if(APP.initial_run){
require(['functions/ajaxify'], function(module){
module.init();
});
}
APP.initial_run = false;
}
},
//ARTICLE CONTROLLER
article: {
load: function() {
},
do_something_action: function() {
}
}
};
//BEHAVIOURS
/* look through the document (or ajax'd in content if "context" is defined) to look for "data-behavior" attributes.
Initialize a new instance of the method if found, passing through the element that had the attribute
So in this example it will find 'data-behavior="show_articles"' and run the show_articles method.
*/
APP.LoadBehavior = function(context){
if(context === undefined){
context = $(document);
}
context.find("*[data-behavior]").each(function(){
var that = $(this);
var behaviors = that.attr('data-behavior');
$.each(behaviors.split(" "), function(index,behaviorName){
require(['behaviors/' + behaviorName], function(module){
//Init Module
try {
module.init(that);
}
catch(e){
// No Operation
log('error init: ' + e);
}
});
});
});
};
UTIL = {
exec: function( controller, action ) {
var ns = APP,
action = ( action === undefined ) ? "init" : action;
if ( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) {
ns[controller][action]();
}
},
init: function() {
var body = document.body,
controller = body.getAttribute( "data-controller" ),
action = body.getAttribute( "data-action" );
UTIL.exec( "common" );
UTIL.exec( controller );
UTIL.exec( controller, action );
}
};
//INIT
require(['jquery', 'libs/prefixfree.min'], function($){
$(document).ready(function(){
UTIL.init();
});
});
// UTILS
APP.ua = navigator.userAgent;
APP.click_event = (is_touch_device()) ? "touchstart" : "click";
// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
log.history = log.history || []; // store logs to an array for reference
log.history.push(arguments);
if(this.console){
console.log( Array.prototype.slice.call(arguments) );
}
};
function isIpad() {
return !!navigator.userAgent.match(/iPad/i);
};
function isIphone () {
return !!navigator.userAgent.match(/iPhone/i);
};
function isIpod() {
return !!navigator.userAgent.match(/iPod/i);
};
function isAppleIos() {
return (isIpad() || isIphone() || isIpod());
};
function is_touch_device() {
return !!('ontouchstart' in window) // works on most browsers
|| !!('onmsgesturechange' in window); // works on ie10
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment