javascript - Dynamic segments with default routes in Iron Router? -


in meteor, using iron-router, i'm trying implement route mapping has dynamic segment , fallback route if dynamic segment not match items in collection. example, have url so:

http://foobar.com/harold

i first check if harold matches ids in posts collection. if there match should take me postpage template.

if there no matches, router should render harold matches items.

i've searched through iron-router documentation, can't seem figure out right approach. wonder if there this.next() cancels current route mapping , goes next route mapping. here's attempt try it:

router.map(function () {   this.route('postpage', {     // matches: '/mfls6akqqrz2jgz9q'     // matches: '/81zbqge85aafjk1js'     path: '/:postid',     before: function () {       //check if segment matches post id       post = posts.findone(this.params.postid);       if (!post) {         //if no matches, go next route         //something this.next()?       }     },    data: function() {       return posts.findone(this.params.postid);    }   });    this.route('profilepage', {     // matches: '/harold'     // matches: '/brendan'     path: '/:username',     data: function() {       return profiles.findone(this.params.username);     }   }); }); 

what you're describing 1 route: /:input input can anything. @david weldon mentioned, isn't idea; subverts point of using router, , causes lot of code , database queries run every time url changes.

that said, if want way, need collapse code 1 route:

router.map(function () {   this.route('theoneroutetorulethemall', {     path: '/:input',     data: function() {       if (profiles.findone({username: this.params.input}) != null)         return profiles.findone({username: this.params.input});       else if (posts.findone({postid: this.params.input} != null)         return posts.findone({postid: this.params.input};       else         return null; // no user or post found; handle notfoundtemplate     }   }); }); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -