Skip to content

Commit

Permalink
feat(RouteSet): Refactored route set to expose query params
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRyanDev committed Apr 1, 2016
1 parent cc01ac2 commit 0f9c40d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
14 changes: 9 additions & 5 deletions lib/route-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'rxjs/add/operator/observeOn';
import { Observable } from 'rxjs/Observable';
import { queue } from 'rxjs/scheduler/queue';
import { provide, Provider, Injector, OpaqueToken } from 'angular2/core';
import { parse as parseQueryString } from 'query-string';

import { compose } from './util';
import { Location, LocationChange } from './location';
Expand All @@ -27,6 +28,7 @@ export const useRouteSetMiddleware = provideMiddlewareForToken(ROUTE_SET_MIDDLEW
export interface NextRoute {
routes: Routes;
params: any;
query: any;
url: string;
}

Expand All @@ -35,7 +37,6 @@ export class RouteSet extends Observable<NextRoute>{ }


function createRouteSet(
routes: Routes,
location$: Location,
traverser: RouteTraverser,
locationMiddleware: Middleware[],
Expand All @@ -45,12 +46,15 @@ function createRouteSet(
.observeOn(queue)
.distinctUntilChanged((prev, next) => prev.url === next.url)
.let<LocationChange>(compose(...locationMiddleware))
.switchMap(change => {
return traverser.matchRoutes(routes, change.url)
.switchMap(() => {
const [ pathname, queryString ] = location$.path().split('?');

return traverser.find(pathname)
.map<NextRoute>(set => {
return {
url: change.url,
url: location$.path(),
routes: set.routes,
query: parseQueryString(queryString),
params: set.params
};
});
Expand All @@ -64,7 +68,7 @@ function createRouteSet(

export const ROUTE_SET_PROVIDERS = [
provide(RouteSet, {
deps: [ ROUTES, Location, RouteTraverser, LOCATION_MIDDLEWARE, ROUTE_SET_MIDDLEWARE ],
deps: [ Location, RouteTraverser, LOCATION_MIDDLEWARE, ROUTE_SET_MIDDLEWARE ],
useFactory: createRouteSet
}),
useLocationMiddleware(identity),
Expand Down
3 changes: 2 additions & 1 deletion lib/route-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class RouteView implements OnDestroy, OnInit{
return {
url: set.url,
routes: [ ...set.routes ].slice(1),
params: set.params
params: set.params,
query: set.query
}
})
});
Expand Down
18 changes: 12 additions & 6 deletions spec/redirect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('Redirect Middleware', function() {
routeSet$.next({
routes: [ { redirectTo: to } ],
params,
url: from
url: from,
query: {}
});
}

Expand All @@ -38,7 +39,8 @@ describe('Redirect Middleware', function() {
const nextRoute: NextRoute = {
routes: [ { path: '/first' } ],
params: {},
url: '/first'
url: '/first',
query: {}
};

redirect(routeSet$).subscribe(value => {
Expand All @@ -55,7 +57,8 @@ describe('Redirect Middleware', function() {
routeSet$.next({
routes: [ { redirectTo: '/test' }],
params: {},
url: '/go'
url: '/go',
query: {}
});

expect(observer.next).not.toHaveBeenCalled();
Expand All @@ -68,7 +71,8 @@ describe('Redirect Middleware', function() {
routeSet$.next({
routes: [ { redirectTo: '/posts/:id' } ],
params: { id: '543' },
url: '/blog/543'
url: '/blog/543',
query: {}
});

expect(observer.next).not.toHaveBeenCalled();
Expand All @@ -81,7 +85,8 @@ describe('Redirect Middleware', function() {
routeSet$.next({
routes: [ { path: '/first' }, { path: 'second', redirectTo: '/home' } ],
params: {},
url: '/first/second'
url: '/first/second',
query: {}
});

expect(observer.next).not.toHaveBeenCalled();
Expand All @@ -94,7 +99,8 @@ describe('Redirect Middleware', function() {
routeSet$.next({
routes: [ { path: '/blog' }, { path: ':id', redirectTo: '/posts/:id' } ],
params: { id: '543' },
url: '/blog/543'
url: '/blog/543',
query: {}
});

expect(observer.next).not.toHaveBeenCalled();
Expand Down
6 changes: 3 additions & 3 deletions spec/route-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('RouteParams Service', function() {
done();
});

routeSet$.next({ routes: [], params, url: '' });
routeSet$.next({ routes: [], params, url: '', query: {} });
});

it('should let you select a single param to observe', function(done) {
Expand All @@ -43,12 +43,12 @@ describe('RouteParams Service', function() {
done();
});

routeSet$.next({ params, routes: [], url: '' });
routeSet$.next({ params, routes: [], url: '', query: {} });
});

it('should replay the previous param value when you subscribe', function(done) {
const params = { id: 456 };
routeSet$.next({ params, routes: [], url: '' });
routeSet$.next({ params, routes: [], url: '', query: {} });

params$.subscribe(value => {
expect(value).toBe(params);
Expand Down

0 comments on commit 0f9c40d

Please sign in to comment.