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
Routing #15
Routing #15
Conversation
I think we can write a function like var render = require('../browser/app').render;
var router = require('plastiq/router');
app.get('*', function (req, res) {
router.respond(req, res, render, { user: req.user });
} |
Yeah I like where this is going. I guess we can use an iframe to get karma tests round it. |
Yeah good point. I was thinking of firing up selenium but if we can do it with an iframe then much better. |
Just did a test with Karma. Tests are already run inside an iframe and pushState and popstate work perfectly. |
h('h1', 'objects'), | ||
h('ul', | ||
objects.map(function (o) { | ||
return router.link('/objects/' + o.id, h('li', o.name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
router.link()
generates an anchor tag, so I guess you meant to nest the link inside the h('li')
.
I wonder if regular anchors (as in h('a', ...)
) should automatically plug into the router. That seems like it would be a good reason for routing to be integrated into plastiq itself. The usual reason for using something like router.link
instead of regular anchors is to plug into some "named routes" system, e.g.
router.link('showObject', { id = o.id }, o.name)
Personally I like named routes on bigger projects because: 1) the framework can throw an error when you link to bad URLs instead of when you follow those links and 2) your code is isolated from changes to the URL.
However mandatory route names complicate route setup code. Perhaps a middle ground is that routes could have name == pattern by default. That would allow this:
router.link('/objects/:id', { id: o.id }, o.name)
...or even this:
router.link('/objects/:id', o.id, o.name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I like having validated routes for link
, and I think having the pattern there is more "real". You can always make a function like showObject(o.id) => "/objects/" + o.id
, if it bothers you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking of refactoring from a regular anchor, another option:
h('a', { href: ['/objects/:id', o.id] }, ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember being quite confused when angular automatically hijacked all my anchor tags. I wasn't happy about it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree, too surprising, annoying in some cases.
But I do like the idea of routing anchors looking pretty similar to regular anchors. This may be less funky:
h('a', { route: ['/objects/:id', o.id] }, ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's it.
So, we have a |
Sounds good to me. When you say "overriding", does that mean "cancelling any existing onclick"? I was wondering about this with the binding attribute too... |
yeah, in the In the case of the onclick here, especially with anchors, is that we would have to |
Some changes to the API:
In effect, we have the following features:
|
woot |
Run the example:
Then hit http://localhost:4000/
You'll notice that there is a 300ms delay when loading the
/objects
and/objects/:id
pages. This is to simulate an AJAX load time. However, when navigating forward and back we usehistory.state
so there is no delay. When we refresh the page (F5, ⌘-R), we fetch new state instead of usinghistory.state
.