Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simple router used in a basic project.
/**
* Router
* @class Router
* @author gneutzling
* @date 2013-12-20
*
* Class.js by John Resig is needed. (http://ejohn.org/blog/simple-javascript-inheritance/)
*/
APP.core.Router = Class.extend({
init: function() {
this.setup();
this.bind();
},
setup: function () {
this.url = null;
this.routes = {
'/': { controller: 'HomeController', templateId: '#home' },
'/about': { controller: 'AboutController', templateId: '#about' },
'/contact': { controller: 'ContactController', templateId: '#contact' }
};
},
bind: function () {
var _this = this;
jQuery(window).on('hashchange load', function () {
_this.url = APP.i.util.getHash();
_this.loadController();
});
},
loadController: function () {
if (this.routes[this.url]) {
new APP.controller[this.routes[this.url].controller];
} else {
APP.i.util.setHash('/');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.