Created
August 5, 2013 23:45
-
-
Save ericf/6160679 to your computer and use it in GitHub Desktop.
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
diff --git a/src/app/js/router.js b/src/app/js/router.js | |
index 3b91967..8b8cc6f 100644 | |
--- a/src/app/js/router.js | |
+++ b/src/app/js/router.js | |
@@ -9,6 +9,7 @@ Provides URL-based routing using HTML5 `pushState()` or the location hash. | |
var HistoryHash = Y.HistoryHash, | |
QS = Y.QueryString, | |
YArray = Y.Array, | |
+ YObject = Y.Object, | |
win = Y.config.win, | |
@@ -98,6 +99,14 @@ Y.Router = Y.extend(Router, Y.Base, { | |
**/ | |
/** | |
+ TODO: Add docs! | |
+ | |
+ @property _params | |
+ @type Object | |
+ @protected | |
+ **/ | |
+ | |
+ /** | |
Whether or not the `ready` event has fired yet. | |
@property _ready | |
@@ -144,11 +153,20 @@ Y.Router = Y.extend(Router, Y.Base, { | |
**/ | |
_regexUrlOrigin: /^(?:[^\/#?:]+:\/\/|\/\/)[^\/]*/, | |
+ /** | |
+ TODO: Add docs! | |
+ | |
+ @property _routes | |
+ @type Array | |
+ @protected | |
+ **/ | |
+ | |
// -- Lifecycle Methods ---------------------------------------------------- | |
initializer: function (config) { | |
var self = this; | |
self._html5 = self.get('html5'); | |
+ self._params = {}; | |
self._routes = []; | |
self._url = self._getURL(); | |
@@ -301,6 +319,13 @@ Y.Router = Y.extend(Router, Y.Base, { | |
}, | |
/** | |
+ TODO: Add docs! | |
+ **/ | |
+ param: function (name, regex) { | |
+ this._params[name] = regex; | |
+ }, | |
+ | |
+ /** | |
Removes the `root` URL from the front of _url_ (if it's there) and returns | |
the result. The returned path will always have a leading `/`. | |
@@ -621,7 +646,7 @@ Y.Router = Y.extend(Router, Y.Base, { | |
decode = self._decode, | |
routes = self.match(path), | |
callbacks = [], | |
- matches, req, res; | |
+ matches, paramsMatch, req, res; | |
self._dispatching = self._dispatched = true; | |
@@ -673,10 +698,32 @@ Y.Router = Y.extend(Router, Y.Base, { | |
return match && decode(match); | |
}); | |
+ paramsMatch = true; | |
+ | |
// Use named keys for parameter names if the route path contains | |
// named keys. Otherwise, use numerical match indices. | |
if (matches.length === route.keys.length + 1) { | |
- req.params = YArray.hash(route.keys, matches.slice(1)); | |
+ matches = matches.slice(1); | |
+ req.params = YArray.hash(route.keys, matches); | |
+ | |
+ paramsMatch = YArray.every(route.keys, function (key, i) { | |
+ var paramRegex = self._params[key], | |
+ value = matches[i], | |
+ captures; | |
+ | |
+ if (paramRegex && value && typeof value === 'string') { | |
+ captures = paramRegex.exec(value); | |
+ | |
+ if (captures) { | |
+ req.params[key] = captures; | |
+ return true; | |
+ } | |
+ | |
+ return false; | |
+ } | |
+ | |
+ return true; | |
+ }); | |
} else { | |
req.params = matches.concat(); | |
} | |
@@ -685,8 +732,13 @@ Y.Router = Y.extend(Router, Y.Base, { | |
// request. | |
req.pendingRoutes = routes.length; | |
- // Execute this route's `callbacks`. | |
- req.next(); | |
+ if (paramsMatch) { | |
+ // Execute this route's `callbacks`. | |
+ req.next(); | |
+ } else { | |
+ // Skip this route because the param regexps don't match. | |
+ req.next('route'); | |
+ } | |
} | |
}; | |
@@ -733,6 +785,13 @@ Y.Router = Y.extend(Router, Y.Base, { | |
}, | |
/** | |
+ TODO: Add docs! | |
+ **/ | |
+ _getParams: function () { | |
+ return Y.merge(this._params); | |
+ }, | |
+ | |
+ /** | |
Gets the current route path, relative to the `root` (if any). | |
@method _getPath | |
@@ -1203,6 +1262,19 @@ Y.Router = Y.extend(Router, Y.Base, { | |
}, | |
/** | |
+ TODO: Add docs! | |
+ **/ | |
+ _setParams: function (params) { | |
+ this._params = {}; | |
+ | |
+ YObject.each(params, function (regex, name) { | |
+ this.param(name, regex); | |
+ }, this); | |
+ | |
+ return Y.merge(this._params); | |
+ }, | |
+ | |
+ /** | |
Setter for the `routes` attribute. | |
@method _setRoutes | |
@@ -1336,6 +1408,15 @@ Y.Router = Y.extend(Router, Y.Base, { | |
}, | |
/** | |
+ TODO: Add docs! | |
+ **/ | |
+ params: { | |
+ value : {}, | |
+ getter: '_getParams', | |
+ setter: '_setParams' | |
+ }, | |
+ | |
+ /** | |
Absolute root path from which all routes should be evaluated. | |
For example, if your router is running on a page at |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment