Created
September 3, 2015 22:10
-
-
Save caiotarifa/800bb7f356d3e379127c to your computer and use it in GitHub Desktop.
Vanilla JavaScript Dispatcher
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
var Dispatcher = (function() { | |
'use strict'; | |
var route, namespaceName, controllerName, controller, actionName, action; | |
function capitalize(string) { | |
return string.charAt(0).toUpperCase() + string.substring(1); | |
} | |
route = $('body').data('route').split('#'); | |
controllerName = route[0].split('_'); | |
if (controllerName.length > 1) { | |
namespaceName = capitalize(controllerName[0]); | |
controllerName = capitalize(controllerName[1]); | |
} else { | |
namespaceName = ''; | |
controllerName = capitalize(controllerName[0]); | |
} | |
controller = window[namespaceName + controllerName + 'Controller']; | |
if (typeof controller !== 'undefined') { | |
controller = controller(); | |
actionName = route[1] + 'Action'; | |
action = controller[actionName]; | |
if (typeof action !== 'undefined') { action(); } | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment