Skip to content

Method for generating URLs from states #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<keys.length; i++) {
var k = keys[i];
Expand Down
28 changes: 26 additions & 2 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ describe('state', function () {
.state('C', C)
.state('D', D)
.state('DD', DD)
.state('E', E);
.state('E', E)

.state('home', { url: "/" })
.state('home.item', { url: "front/:id" })
.state('about', { url: "/about" })
.state('about.person', { url: "/:person" })
.state('about.person.item', { url: "/:id" });

$provide.value('AppInjectable', AppInjectable);
}));
Expand Down Expand Up @@ -226,7 +232,7 @@ describe('state', function () {
}));
});


describe('.transition', function () {
it('is null when no transition is taking place', inject(function ($state, $q) {
expect($state.transition).toBeNull();
Expand All @@ -240,4 +246,22 @@ describe('state', function () {
expect($state.transition).toBe(trans);
}));
});


describe('.href()', function () {
it('aborts on un-navigable states', inject(function ($state) {
expect(function() { $state.href("A"); }).toThrow("State 'A' is not navigable");
}));

it('generates a URL without parameters', inject(function ($state) {
expect($state.href("home")).toEqual("/");
expect($state.href("about", {})).toEqual("/about");
expect($state.href("about", { foo: "bar" })).toEqual("/about");
}));

it('generates a URL with parameters', inject(function ($state) {
expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
expect($state.href("about.person.item", { person: "bob", id: null })).toEqual("/about/bob/");
}));
});
});