Closed thegecko closed 9 years ago
You can achieve something close to 2 with inline templates https://github.com/erikringsmuth/app-router#inline-template.
<link rel="import" href="components/home-page.html">
...
<app-router>
<app-route path="/" template>
<template><home-page title="Welcome"></home-page></template>
</app-route>
...
</app-router>
The template tag prevents things like the created
function from being called until the route is activated.
I want to keep the router as separated from the display of the view as possible. I think the 2 way has the best separation of concerns.
Nice, that works well. I wonder if the template attribute marker could be factored out...
Import a custom element (uses import attribute):
<app-route path="/customer/:customerId" import="/pages/customer-page.html"></app-route>
Pre-loaded custom element (uses element attribute):
<head>
<link rel="import" href="/pages/page-bundle.html">
</head>
<app-router>
<app-route path="/customer/:customerId" element="customer-page"></app-route>
</app-router>
Pre-loaded inline custom element (first element of content is not a template and allows further attributes):
<head>
<link rel="import" href="/pages/page-bundle.html">
</head>
<app-router>
<app-route path="/customer/:customerId">
<customer-page name="bob"></customer-page>
</app-route>
</app-router>
Import template (template attribute used for path):
<app-route path="/example" template="/pages/template-page.html"></app-route>
Inline template (first content element is 'template'):
<app-route path="/example">
<template>
<p>Inline template FTW!</p>
</template>
</app-route>
Just a thought, with absolutely no consideration for how it could be implemented!
This gave me some ideas. Now I spent all morning trying to remove the template
attribute and auto-detect if it's a template or custom element. I'm getting close but it's not quite working yet.
https://github.com/erikringsmuth/app-router/tree/simplify-imports
I don't think I'll be able to get the pre-loaded inline custom elements working. The template tag is necessary when you inline any code otherwise it will display right away and execute created and attached callbacks before the route is activated.
This set of changes will still take a while but I'll keep working on them over the next week.
Thanks, I'll take a look
I see what you mean. I think a wrapper is the only way of holding off element activation.
I guess the route content could be hidden using some other trick while inline elements are activated, but I'm not sure this is worth the effort bearing in mind the same thing can be accomplished by wrapping it in a template as you initially suggested.
Good effort!
Yeah, I just tried one more thing to have each <app-route>
wrap it's content in a template. Imagine something like this.
<polymer-element name="app-route">
<template>
<template id="route-content">
<content><!-- This is where I was hoping the content would end up. --></content>
</template>
</template>
</polymer-element>
Later I would clone the route-content template and set it as the active route
The <content>
tag is where I would expect content (<customer-page name="bob"></customer-page>
) to be placed. The problem is that the <content>
tag isn't part of the <app-route>
's DOM when the element is created because I haven't cloned the route-content
yet. It's a catch 22. I need the template to be inert to prevent the customer-page
from loading, but I need it active to use the <content>
tags inside of it.
This article explains what the <template>
tag is doing. The big difference from Angular or Backbone templates is that <template>
tags are parsed, inert DOM that can be cloned into the real DOM. Angular and Backbone's templates are strings that have to be parsed every time you render them.
http://www.html5rocks.com/en/tutorials/webcomponents/template/#toc-pillars
I made the change to not require the template
attribute on the inline template routes. You still need the <template>
tag but not the attribute on the route.
I pushed v0.8.0 with the change. https://github.com/erikringsmuth/app-router#inline-template
I was trying to also auto detect a template vs. a custom element in an import but it's not possible as far as I can tell. I thought I had a hack to do it but every browser works differently :cry:. It's now on my "want" list.
We can't always have everything :) The interface is definitely nicer without markers. now about that regex one... just kidding!
Oh man! The regex
one is even harder than template
! Is /path/i
a regular expression that matches path
or a path that matches /path/i
? It turns out a lot of regular expressions look exactly like paths.
Perhaps you can just replace 'path=...' with 'regex=..':
Instead of:
<app-route path="/^\/\w+\/\d+$/i" regex import="/pages/regex-page.html"></app-route>
Have:
<app-route regex="/^\/\w+\/\d+$/i" import="/pages/regex-page.html"></app-route>
But it's a matter of personal preference.
So there is now way how to pass a variable into imported component? Like @thegecko asked in (1)
There is a workaround. It's not as straight forward as setting attributes on the app-route
.
There is a before-data-binding
event.
https://erikringsmuth.github.io/app-router/#/events#before-data-binding
And a Polymer event mapping example. https://erikringsmuth.github.io/app-router/#/events#polymer-event-mapping
It would be very useful to be able to pass attributes through to the imported component. Syntactically, I envisage this could work in two ways. Using an attribute called 'title' as an example...
1 - Passing all unknown attributes on to the imported component:
2 - Allowing the route to contain an instantiated component with the attribute added:
Nice component BTW!