在FlowRouter中定义默认渲染模板的好方法 [英] A good way to define default rendering templates in FlowRouter

查看:103
本文介绍了在FlowRouter中定义默认渲染模板的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当迁移到新的Meteor项目(该项目将Flow-Router替换为Iron-Router)时,我真的错过了Iron-Router定义全局可用默认行为和设置的功能.

When migrating to a new Meteor project which was replacing Iron-Router with Flow-Router, I really missed the functionality of Iron-Router to define default behaviours and settings that were usable globally.

我了解Flow-Router使用组"构造来定义默认钩子事件,该事件可以应用于连接到该组的任何路由,但是似乎没有可用的东西来定义默认模板.

I understand Flow-Router uses the "group" construct to be able to define default hook events which can be applied to any route attached to that group, but there seemed to be nothing available for defining default templates.

实现这种类型的默认全局或组模板功能的最佳方法是什么?

What is the best way to implement this type of default global or group template feature?

推荐答案

更新如下:

我已经找到了我认为是一种相当有效的方法.

I have worked out what I think is a fairly effective and efficient way to do this.

在回顾Flow-Router的功能时,我注意到路由器对象在FlowRouter.current().route.group.options

In reviewing the functionality of Flow-Router, I noticed that the router object includes a field which contains all group parameters, in FlowRouter.current().route.group.options

此对象包含组中定义的任何键值对,无论它们是否被Flow-Router API使用.

This object contains any key-value pair defined in the group, regardless of whether they are used by the Flow-Router API or not.

然后,我决定使用此方法作为将一些模板默认值从组传递到路由器的简单方法.

I then decided to use this as a simple way of passing some template defaults from the group to the router.

首先是组定义:

var mainRoutes = FlowRouter.group({
  name: "mainRoutes",
  defaultTemplates: {
    menu: "mainMenu",
    breadcrumb: "mainCrumb",
    sidebarLeft: "sidebarLeft",
    sidebarRight: "sidebarRight",
    footer: "mainFooter"
  }
});

然后在定义路由时,我们可以从this.group.options.defaultTemplates

Then in defining the route we can access these from this.group.options.defaultTemplates

mainAppRoutes.route('/', {
  name: "home",
  action: function() {
    // Get the defaultTemplates object
    var templates = this.group.options.defaultTemplates;
    // Add route specific templates
    templates.content = "homeScreen";
    // Pass all the templates to the renderer
    BlazeLayout.render("appLayoutMain", templates);
  }
});

我喜欢这种模式,因为它使用了Flow-Router的现有功能以及路由中的一行额外代码,同时允许定义组范围的默认模板.这实质上模仿了Iron-Router在Router.config()中使用模板默认值的方式,而本地路由模板定义的附加好处是不会覆盖整个默认模板对象.

I like this pattern because it uses existing features of Flow-Router, and one extra line of code in the route, while allowing for defining of group wide default templates. This essentially mimics how Iron-Router was using template defaults in Router.config(), with the added benefit of local route template definitions not overwriting the whole default template object.

  • 更新 上面对我来说效果很好,尽管我确实做到了
  • UPDATE The above has worked well for me, although I did end up having another problem which I had to solve to make it work consistently.

本质上,如果您在执行操作时修改任何基础的路由默认值",则在本地route.action函数中,由于Flow-Router是非反应性的,路由器组对象似乎在路由之间保持不变路线在同一组中.同样,此选项"对象也从路由定义传递到路由器.因此,这意味着如果在上述情况下覆盖默认参数,则在调用覆盖该参数的路由之后,该参数将被使用该组的所有后续路由覆盖.

Essentially, if you modify any underlying "route defaults" as we are doing, in the local route.action function, since Flow-Router is non-reactive, the router group object seems to persist from route to route as long as the routes are in the same group. Also this "options" object is passed to the router from the route definition. So what this means is that if you override a default parameter in the case above, it will be overwritten for all subsequent routes using that same group, after the route that overrides it is called.

我想出的解决方案是执行默认的重置功能,如下所示:组定义中的triggersExit: [ resetDefaults ].

The solution I came up with was to do a defaults reset function as so: triggersExit: [ resetDefaults ] in the group definition.

然后只是重置默认值.我在退出时执行此操作的原因是,这样,下一条被调用的路由仍可以在本地覆盖这些默认设置:

Then is just do a resetting of defaults. The reason I did it on exit is so that the next route being called can still override locally these defaults:

function resetDefaults (context) {
  context.route.group.options.defaultLayoutTemplate = "mainAppLayout";
  context.route.group.options.defaultTemplates = {
    defaultTeamplates: "navMain",
    // ...
};

这篇关于在FlowRouter中定义默认渲染模板的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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