javascript - Why do I need to call a dynamic route ":_id" and not whatever I want? -
here's rather standard way set route in iron router:
router.route('/posts/:_id', { name: 'postpage', data: function() { return posts.findone({_id: this.params._id}) } });
experimenting around little, beginner am, tried:
router.route('/posts/:whatever', { name: 'postpage', data: function() { return posts.findone({_id: this.params.whatever}) } });
this works well, point. true, whatever
scoop whatever after /posts/
value, , data context indeed same before... linking specific posts won't work!
so,
<a href="{{pathfor 'postpage'}}">{{title}}</a>
simply won't work doing "my" way (linking nothing @ all).
i can't wrap head around this, , i'm of novice grasp source code iron router, hope here can explain in manner beginner me can comprehend.
preferably this:
first
{{pathfor 'postpage'
}} looks inside routes find 1 namedpostpage
.it sees route corresponds
/posts/
followed else.looking inside data context finds 1 post returned, namely 1 same
_id
whatever comes after/posts/
.it understands should link post, cleverly setting url
/posts/_id
.
this wrong, likely, , doesn't explain why work when whatever
turned _id
. me immensely see parsed in similar fashion.
edit: cleaned question easier grasp.
there's simple set of circumstances lead confusion:
- the
posts.findone
issue explained fact first argument can either selector or document_id
. it's not shortcut rather documented feature. - as found, putting
:something
in iron:router url causes value reportedthis.params.something
inside route function. also registerssomething
parameter route, brings howpathfor
works. - the
pathfor
helper takes 2 inputs: first name of route (in case'postpage'
) , second object of parameters, can come either second argument in{{pathfor 'postpage' params}}
or data context so:{{#with params}}{{pathfor 'postpage'}}{{/with}}
.
now, here's why passing in document database works if call parameter _id
not if call whatever
: post object retrieved database _has _id
field, doesn't have whatever
field. when pass pathfor
, passes along correct _id
if parameter route happens called _id
.
let me know if makes sense, agree confusing , "shortcut" hides pathfor
, params
do.
Comments
Post a Comment