Skip to content

Replace m.route by a Router component #2587

@andershol

Description

@andershol

Is this something you're interested in implementing yourself? No

Description

This is just a rough idea. Maybe it would be useful to instead of writing
m.route(document.body, "/page1", { "/page1": Page1 });
to be able to write something like:
m.mount(document.body, { view: () => m(Router, { default: '/page1', routes: { "/page1": Page1 } }) });
such that routing could be done inside layouts and not only at the top level. It might also allow routing to be split into components and nested.

A larger sample (the implementation is a quick hack), tested in Firefox and chrome:

  • Currently the m.route function is used something like sample 1. Here an application have three pages that share a common layout.
  • If m.route was replaced by a route component, that sample might be directly translated onto sample 2
  • This means that the router could be moved inside the Layout component, so the individual pages could be animated (this is my current primary use-case) as in sample 3
  • And it might even be possible to nest the Routers, so the routes could be put inside the components to which they belong as in sample 4. But this means that a route like "/page1" also match the route "/page1/tab1" which might not be desirable.

Why

Sample 3 is my motivation. It seems to me that I in this case want to not have the routing on the top level, but instead inside the layout.
Some of the motivation is that it seems that the diff-algorithm will not (refer to sample 1) match up the Layout component used in Page1 to the Layout component used in Page2, so the Layout is not updated, but removed and replaced, making animation of the content imposible.

Open Questions

It seems the implementation could be made less hackish if there was a onafterview method, that was called after all child view methods had been called, or if the view method for the child components was called by the m-method (i.e. so the view methods for the child components was called and completed inside the parent view method).
The use of target:'_Self' is a hack (that at least work in Firefox and Chrome) to have m.route.Link use the href and not its own onclick as the onclick use a new value for m.route.prefix, not the one used to set href.

Metadata

Metadata

Assignees

Labels

Area: CoreFor anything dealing with Mithril core itselfType: EnhancementFor any feature request or suggestion that isn't a bug fix

Type

No type
No fields configured for issues without a type.

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions