Created
September 3, 2011 07:25
-
-
Save draeton/1190785 to your computer and use it in GitHub Desktop.
Playing at a simple app framework
This file contains 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
(function (window) { | |
var document = window.document; | |
var getType = function (thing) { | |
return Object.prototype.toString.call(thing).match(/\s(\w+)/)[1].toLowerCase(); | |
}; | |
var getIsType = function (type) { | |
return (function (thing) { | |
return getType(thing) === type; | |
}); | |
}; | |
var isObject = getIsType('object'); | |
var isArray = getIsType('array'); | |
var isFunction = getIsType('function'); | |
var getParams = function () { | |
var search, pairs, pair, idx, val, params, i, l; | |
search = document.location.search.replace('?', ''); | |
pairs = search.split('&'); | |
params = {}; | |
for (i = 0, l = pairs.length; i < l; i++) { | |
pair = pairs[i].split('='); | |
idx = pair[0]; | |
val = pair[1]; | |
if (params.hasOwnProperty(idx)) { | |
if (isArray(params[idx])) { | |
params[idx].push(val); | |
} else { | |
params[idx] = [params[idx], val]; | |
} | |
} else { | |
params[idx] = val; | |
} | |
} | |
return params; | |
}; | |
var globalRoutes = {}; | |
var addRoutes = function (routes) { | |
var i; | |
if (isObject(routes)) { | |
for (i in routes) { | |
if (routes.hasOwnProperty(i)) { | |
globalRoutes[i] = routes[i]; | |
} | |
} | |
} | |
}; | |
var router = function () { | |
var loc, route, params; | |
loc = document.location; | |
route = loc.pathname; | |
params = getParams(); | |
if (isFunction(globalRoutes[route])) { | |
globalRoutes[route].call(this, params); | |
} | |
}; | |
window.addRoutes = addRoutes; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment