From ddbef6519a465cc73c7bad43e327f711be88fe5c Mon Sep 17 00:00:00 2001 From: Nate Abele Date: Wed, 22 May 2013 14:12:51 -0400 Subject: [PATCH] Implementing href() method for generating state URLs. --- src/state.js | 22 ++++++++++++++++------ test/stateSpec.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/state.js b/src/state.js index 92ec891e9..b4c26ffdf 100644 --- a/src/state.js +++ b/src/state.js @@ -184,12 +184,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { } // Normalize/filter parameters before we pass them to event handlers etc. - var normalizedToParams = {}; - forEach(to.params, function (name) { - var value = toParams[name]; - normalizedToParams[name] = (value != null) ? String(value) : null; - }); - toParams = normalizedToParams; + toParams = normalize(to.params, toParams || {}); // Broadcast start event and cancel the transition if requested if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams) @@ -271,6 +266,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { return $state.$current.includes[findState(stateOrName).name]; }; + $state.href = function (stateOrName, params) { + var state = findState(stateOrName), nav = state.navigable; + if (!nav) throw new Error("State '" + state + "' is not navigable"); + return nav.url.format(normalize(state.params, params || {})); + }; function resolveState(state, params, paramsAreFiltered, inherited, dst) { // We need to track all the promises generated during the resolution process. @@ -345,6 +345,16 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { }); } + function normalize(keys, values) { + var normalized = {}; + + forEach(keys, function (name) { + var value = values[name]; + normalized[name] = (value != null) ? String(value) : null; + }); + return normalized; + } + function equalForKeys(a, b, keys) { for (var i=0; i