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:

  1. first {{pathfor 'postpage'}} looks inside routes find 1 named postpage.

  2. it sees route corresponds /posts/ followed else.

  3. looking inside data context finds 1 post returned, namely 1 same _id whatever comes after /posts/.

  4. 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:

  1. the posts.findone issue explained fact first argument can either selector or document _id. it's not shortcut rather documented feature.
  2. as found, putting :something in iron:router url causes value reported this.params.something inside route function. also registers something parameter route, brings how pathfor works.
  3. 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

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -