Skip to content

Instantly share code, notes, and snippets.

@gneutzling
Created March 24, 2014 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gneutzling/9741087 to your computer and use it in GitHub Desktop.
Save gneutzling/9741087 to your computer and use it in GitHub Desktop.
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