流星铁路由器,并为accounts-entry包获取所有路线的名称 [英] meteor iron-router and getting the names of all routes for the accounts-entry package

查看:62
本文介绍了流星铁路由器,并为accounts-entry包获取所有路线的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

流行的 accounts-entry 程序包中有一个与铁路由器相关的错误.我相信铁路由器的更高版本已更改为可以更好地用作中间件,因此可以调用Router.routes

The popular accounts-entry package has an iron-router related bug in it. I believe the later versions of iron-router changed to work better as middleware and so a call to Router.routes

在此文件的第87行使用以下代码:

At line 87 of this file the following code is used:

_.each Router.routes, (route)->
  exclusions.push route.name
# Change the fromWhere session variable when you leave a path
Router.onStop ->
  # If the route is an entry route, no need to save it
  if (!_.contains(exclusions, Router.current().route?.getName()))
    Session.set('fromWhere', Router.current().path)

不幸的是,在Router.routes上执行_.each似乎不再可行,因为Router.routes不会返回并且对象中带有.name属性.

Unfortunately it does not seems like doing an _.each on Router.routes is a solution that works anymore because Router.routes does not return and object with .name properties in it.

如何使用最新的铁路由器获得所有路线的名称?

How would you get the name of all the routes with the latest iron-router?

推荐答案

这有点棘手:在最新版本的iron:router中,Router.routes现在定义为函数数组.

This one is a little tricky : in the latest version of iron:router, Router.routes is now defined as an array of functions.

事实是,函数在JS中已经具有默认的name属性,其中包含在定义时为函数分配的名称.

Thing is, functions already have a default name property in JS which contains the name the function was assigned on definition.

var myFunc = function funcName(){...};
console.log(myFunc.name); // == "funcName"

幸运的是,在数组的路由项上定义了一个getName方法,您可以使用这段代码遍历所有路由并获取其名称:

Fortunately, there is a getName method defined on the route items of the array and you can use this piece of code to iterate over all routes and get their name :

_.each(Router.routes, function(route){
  console.log(route.getName());
});

这篇关于流星铁路由器,并为accounts-entry包获取所有路线的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆