Skip to content

Commit 67af203

Browse files
Fixes issue #8 by adding a globalHash property to the Link component
1 parent 13d79e9 commit 67af203

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

docs/contextual.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ prefixes.
5757
### Navigate outside of the router's context
5858

5959
Sometimes you want to navigate outside of the router's context. You can do this
60-
with `Link` component by creating it with a `global` boolean property:
60+
with `Link` component by creating it with a `global` boolean property, if you are only using `hash` routes use the `globalHash` property:
6161

6262
<Link global href="/">Home</Link>
6363

lib/Link.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var Link = React.createClass({
1818

1919
propTypes: {
2020
href: React.PropTypes.string.isRequired,
21-
global: React.PropTypes.bool
21+
global: React.PropTypes.bool,
22+
globalHash: React.PropTypes.bool
2223
},
2324

2425
onClick: function(e) {
@@ -40,6 +41,10 @@ var Link = React.createClass({
4041
},
4142

4243
_navigate: function(path, cb) {
44+
if(this.props.globalHash) {
45+
return Environment.hashEnvironment.navigate(path, cb);
46+
}
47+
4348
return this.props.global ?
4449
Environment.defaultEnvironment.navigate(path, cb) :
4550
this.navigate(path, cb);

tests/browser.js

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,19 @@ describe('Hash routing', function() {
504504

505505
render: function() {
506506
return Router.Locations({ref: 'router', hash: true, className: 'App'},
507-
Router.Location({
508-
path: '/',
509-
handler: function(props) {
510-
return Router.Link({ref: 'link', href: '/hello'}, 'mainpage')
511-
}
512-
}),
513-
Router.Location({
514-
path: '/:slug',
515-
handler: function(props) {
516-
return div(null, props.slug);
517-
}
518-
})
519-
);
507+
Router.Location({
508+
path: '/',
509+
handler: function(props) {
510+
return Router.Link({ref: 'link', href: '/hello'}, 'mainpage');
511+
}
512+
}),
513+
Router.Location({
514+
path: '/:slug',
515+
handler: function(props) {
516+
return div(null, props.slug);
517+
}
518+
})
519+
);
520520
}
521521
});
522522

@@ -571,3 +571,64 @@ describe('Hash routing', function() {
571571
});
572572

573573
});
574+
575+
describe('Contextual Hash routers', function() {
576+
577+
var SubCat = React.createClass({
578+
579+
render: function() {
580+
return React.DOM.div(null,
581+
Router.Locations({ref: 'router', contextual: true},
582+
Router.Location({
583+
path: '/',
584+
handler: function(props) { return div(null, 'subcat/root') }
585+
}),
586+
Router.Location({
587+
path: '/escape',
588+
handler: function(props) {
589+
return Router.Link({globalHash: true, ref: 'link', href: '/'}, 'subcat/escape');
590+
}
591+
})
592+
));
593+
}
594+
});
595+
596+
var App = React.createClass({
597+
598+
render: function() {
599+
return Router.Locations({ref: 'router', hash: true},
600+
Router.Location({
601+
path: '/',
602+
handler: function() {
603+
return div(null, "mainpage");
604+
}
605+
}),
606+
Router.Location({
607+
path: '/subcat/*',
608+
handler: SubCat,
609+
ref: 'subcat'
610+
})
611+
);
612+
}
613+
});
614+
615+
beforeEach(setUp(App));
616+
afterEach(cleanUp);
617+
618+
describe('Link component', function() {
619+
620+
it('does not scope globalHash Link to a current context', function(done) {
621+
assertRendered('mainpage');
622+
router.navigate('/subcat/escape', function() {
623+
assertRendered('subcat/escape');
624+
clickOn(router.refs.subcat.refs.router.refs.link);
625+
setTimeout(function() {
626+
assertRendered('mainpage');
627+
done();
628+
}, 200);
629+
});
630+
});
631+
632+
});
633+
634+
});

0 commit comments

Comments
 (0)