When using a regex in iron router, how to access the match?

烈酒焚心 提交于 2019-11-28 06:51:18

问题


Rather than having five different routes, I am trying to combined five different paths into a single function, but I still need to know which path it matched. So if I set up my route using a regex:

Router.route(/^\/(accounts)|(contacts)|(forecasts)|(analytics)|(activities)/, function() {
    // which one did it match?
    console.log("matched "+this.params) //this returns an array of five params
}

how do I refer to the matched parameter inside the function?

this.params returns an array of five params with four of them undefined and the other matching, but which one is defined depends on what matched so this isn't very helpful.


回答1:


This is a typical scenario for Iron.Router custom route controller! Check the guide here If you need some code, let me know. ;)

[Edit]

  1. First, you define a route controller for all your sites, which inherit from the RouteController.

    BaseController = RouteController.extend({
        // Put repeating logic for your sites here
    });
    
  2. After that, you define your routes seperatly, not via regex. This is much more modular and maintainable. The big point here is, you don't have to find out by complicated functions, which site is current.

    Router.map(function(){
        this.route('accounts', {
            path: '/accounts'
        });
    });
    
  3. Now, give your routes his own controller

    Router.map(function(){
        this.route('accounts', {
            path: '/accounts',
            controller: 'AccountsController'
        });
    });
    
  4. In this Example, your AccountsController needs to inherit from your new BaseController. Put all your route logic, which is specific for only this route, in your AccountsController

    AccountsController = BaseController.extend({
        // Put your specific route logic here
    });
    
  5. Make sure, your BaseController is loading by meteor before your routes. Check out Meteor File Load Order.You can do something like the following

    client/routes
    client/routes/routes.js
    client/routes/controller/AccountsController.js
    client/routes/controller/BaseController.js
    



回答2:


I was able to use this.params after all by rewriting the regex as follows:

Router.route(/^\/(accounts|contacts|forecasts|analytics|activities)/, function() {
    console.log('matched: '+this.params[0]);
}

Not sure if this is the best way to refer to a regex match tho.




回答3:


I think the best way to do this would be to use a loop to define several routes.

_.each(["accounts", "contacts", "forecasts"], function (path) {
  Router.route(path, function () {
    // inside here you can reference path
  });
});

Using one route with many unrelated paths seems like a hack to me.



来源:https://stackoverflow.com/questions/27455206/when-using-a-regex-in-iron-router-how-to-access-the-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!