当迁移到一个用 Flow-Router 替换 Iron-Router 的新 Meteor 项目时,我真的很怀念 Iron-Router 定义全局可用的默认行为和设置的功能。
我知道 Flow-Router 使用“组”构造来定义默认挂钩事件,这些事件可以应用于附加到该组的任何路由,但似乎没有任何可用于定义默认模板。
实现这种类型的默认全局或组模板功能的最佳方法是什么?
我决定使用一个简单的抽象来模拟 Iron-Router 的此功能。在我考虑在 FlowRouter 本身中更直接地集成此类功能之前,我正在尝试评估这是否是一个合理且可扩展的模式,并与正在寻找类似解决方案的其他人分享这个想法。
我只是创建了一个对象,该对象用作参数对象模板,该模板根据需要进行修改,然后在调用 BlazeLayout.render 时传递。
这是什么routes.js
可能看起来像:
var appRouteGroup = FlowRouter.group({
name: "appRouteGroup",
triggersEnter: [sometrigger]
});
var appRouteTemplates = {
menuMain: "appNav",
footerMain: "appFooter"
};
appRouteGroup.route("home", {
name: "home",
action: function () {
// We get the default templates
var templates = appRouteTemplates;
// Now we can add the route specific template
templates.content = "homePage";
// Then render with templates passed through the object param
BlazeLayout.render("mainLayout", templates);
}
});
然后,这将允许您渲染默认组模板,而无需为每个路由重新定义它们。并使用模板标记如下:
<template name="mainLayout">
{{>Template.dynamic template=menuMain}}
{{>Template.dynamic template=content}}
{{>Template.dynamic template=footerMain}}
</template>
我正在寻找一种方法来在 FlowRouter 中进行更直接的集成,但这似乎功能完美。有什么改进的想法吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)