Created
March 24, 2014 14:25
-
-
Save gneutzling/9741087 to your computer and use it in GitHub Desktop.
a simple router used in a basic project.
This file contains hidden or 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
/** | |
* 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