如何防止本地Flow-Router动作函数覆盖组定义的元素 [英] How to prevent local Flow-Router action function overwriting group defined element

查看:71
本文介绍了如何防止本地Flow-Router动作函数覆盖组定义的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Flow-Router组定义类似于:

I have a Flow-Router group definitions similar to:

var myRouteGroup = FlowRouter.group({
  name: "myGroupName",
  prefix: "/myPrefix",
  // Using arbitrary element to pass group wide defaults
  defaultGroupSettings: {item1: "value1", item2: "value2"};
});

我定义该组中的路线:

myRouteGroup.route("/home",{
  name: "myRoute",
  triggersEnter: [ /*...*/ ],

  action: function () {
    // Get the arbitrary settings object from group definition
    var settings = this.group.options.defaultGroupSettings;
    // Override one of the settings element's value
    settings.item1 = "new value";
    // Render the route, and pass the modified settings
    BlazeLayout.render("layoutTemplate", settings);
  }

});

我想解决的问题。在调用此路由后,上面的代码将覆盖连接到该组的所有后续路由的 defaultGroupSettings.item1 。就好像本地覆盖覆盖组设置对象,或者如果后续路由在同一组中,则不会再次调用组设置对象。

The problem I am trying to solve. The above code overwrites the defaultGroupSettings.item1 for all subsequent routes attached to the group after this route is called. It's as if the local override either overwrites the group settings object, or the group settings object is not called again if subsequent routes are in the same group.

这是一个问题有数据范围?或者,如果被调用的新路由是同一组的一部分,并且只是回收现有的先前路由组对象,那么Flow-Router是不是再次引用组定义的问题?或者也许是我没想过的东西。

It is a problem with scope of the data? Or is it a problem of Flow-Router not referring to the group definition again if the new route being called is a part of the same group, and just recycling the existing previous route group object? Or maybe something I haven't thought of.

推荐答案

更明确地理解这个问题:

The problem more explicitly understood:

Flow路由器呈现路由器对象的group元素,并在引用组选项时使用该对象(似乎),而不是引用或重新运行路由组定义。

Flow router render the "group" element of the router object, and uses that object (it seems) when referencing the group options, rather than reference or re-run the route group definition.

这意味着所有被调用但具有相同组的后续路由似乎只是从前一个路由继承先前渲染的组对象。与它的非反应性方法保持一致。

This means all subsequent routes which are called but have the same group, seem to simply inherit the previously rendered group object from the previous route. In being consistent with it's non-reactive approach.

我开发的方法是添加 triggersExit:[resetDefaults] 到组定义。现在,在组内调用的任何路由都将通过添加函数在路由调用之间重置路由默认值:

The way I developed to deal with this was to add triggersExit: [resetDefaults] to the group definition. Now any route called within the group, will have the route defaults reset between calling of routes by adding the function:

基本上,如果您将任何基础路由默认值修改为我们正在做本地的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.groupDefaultValue = "myVal";
};

我认为这应该很好用,你可以检查讨论此功能的演变。

This should work pretty well I think, and you can check out a discussion of the evolution of this functionality.

这篇关于如何防止本地Flow-Router动作函数覆盖组定义的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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