Basic route stuff working

This commit is contained in:
Max Lynch
2013-09-19 23:48:11 -05:00
parent 1814b0e781
commit 32b59ff372
3 changed files with 70 additions and 13 deletions

View File

@ -2,6 +2,11 @@
* Adapted from Backbone.js * Adapted from Backbone.js
*/ */
(function(window, document, ionic) { (function(window, document, ionic) {
var optionalParam = /\((.*?)\)/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
// Cached regex for stripping a leading hash/slash and trailing space. // Cached regex for stripping a leading hash/slash and trailing space.
var routeStripper = /^[#\/]|\s+$/g; var routeStripper = /^[#\/]|\s+$/g;
@ -15,6 +20,7 @@
this.options = options; this.options = options;
this.root = this.options.root || '/'; this.root = this.options.root || '/';
this.root = ('/' + this.root + '/').replace(rootStripper, '/');
this.handlers = []; this.handlers = [];
@ -25,17 +31,51 @@
}; };
RouteViewController.prototype = { RouteViewController.prototype = {
when: function(route, callback) {
var _this = this;
route = this._routeToRegExp(route);
this.handlers.unshift({
route: route,
callback: function(fragment) {
var args = _this._extractParameters(route, fragment);
callback && callback.apply(_this, args);
}
});
},
// Convert a route string into a regular expression, suitable for matching
// against the current location hash.
_routeToRegExp: function(route) {
route = route.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, function(match, optional){
return optional ? match : '([^\/]+)';
})
.replace(splatParam, '(.*?)');
return new RegExp('^' + route + '$');
},
// Given a route, and a URL fragment that it matches, return the array of
// extracted decoded parameters. Empty or unmatched parameters will be
// treated as `null` to normalize cross-browser behavior.
_extractParameters: function(route, fragment) {
var params = route.exec(fragment).slice(1);
var extracted = [];
for(var i = 0; i < params.length; i++) {
if(param) {
extracted.push(decodeURIComponent(param));
}
}
},
_bindEvents: function() { _bindEvents: function() {
var _this = this; var _this = this;
window.addEventListener('popstate', function(event) { window.addEventListener('popstate', function(event) {
console.log("POP STATE", event, window.location, window.location.hash);
_this.checkUrl(event); _this.checkUrl(event);
}); });
window.addEventListener('pushstate', function(event) {
console.log("PUSH STATE", event, window.location);
});
}, },
checkUrl: function(e) { checkUrl: function(e) {
var current = this.getFragment(); var current = this.getFragment();
@ -44,15 +84,11 @@
}, },
getFragment: function(fragment, forcePushState) { getFragment: function(fragment, forcePushState) {
if (fragment == null) { if (fragment == null) {
if (this._hasPushState || !this._wantsHashChange || forcePushState) {
fragment = this.location.pathname; fragment = this.location.pathname;
var root = this.root.replace(this.trailingSlash, ''); var root = this.root.replace(this.trailingSlash, '');
if (!fragment.indexOf(root)) fragment = fragment.substr(root.length); if (!fragment.indexOf(root)) fragment = fragment.substr(root.length);
} else {
fragment = this.getHash();
} }
} return fragment.replace(routeStripper, '');
return fragment.replace(this.routeStripper, '');
}, },
getHash: function(window) { getHash: function(window) {
var match = (window || this).location.href.match(/#(.*)$/); var match = (window || this).location.href.match(/#(.*)$/);

View File

@ -0,0 +1,14 @@
describe('RouteController', function() {
beforeEach(function() {
});
it('Should init', function() {
var rc = new RouteViewController({
root: 'hacking/route.html';
});
rc.when('/', function() {
console.log('Loaded');
});
});
});

View File

@ -15,13 +15,20 @@
</header> </header>
<main class="has-header content"> <main class="has-header content">
<a href="#cats">Cats</a> <a href="cats/">Cats</a>
</main> </main>
</section> </section>
<script src="RouteController.js"></script> <script src="RouteController.js"></script>
<script> <script>
var rc = new RouteViewController({ var rc = new RouteViewController({
root: 'hacking/'
});
rc.when('route.html', function() {
console.log('Loaded');
});
rc.when('cats', function() {
console.log('Loaded');
}); });
</script> </script>
</body> </body>