问题
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"};
});
I than define a route in that group:
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);
}
});
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.
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.
回答1:
The problem more explicitly understood:
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.
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:
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.
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.
来源:https://stackoverflow.com/questions/38012276/how-to-prevent-local-flow-router-action-function-overwriting-group-defined-eleme