You can see a live demo of this here.
I experimented a little bit with web components and Polymer in the last few days and come up with a routing schema using web components. I would like to know if there are any flaws or shortcomings you can see.
I will start with the basic usage:
demo-app.html
<!-- Create a new component-router and bind the routeData to the local route variable -->
<component-router routeData="{{route}}">
<!-- Define the route(s) this component will be activated on-->
<component-route route="component/:id:/data/:data:"></component-route>
<!-- only load component if something is routed. -->
<template if="{{route && route.id <= 2}}">
<!-- Dynamicly load a component (using HTMLImports) and pass in custom data -->
<component-loader path='component-{{route.id}}.html' data="{{route.data}}" ></component-loader>
</template>
<!-- Show error -->
<template if="{{!route || route.id > 2}}">
<p>Component {{route.id}} not found. There are only two components.</p>
</template>
</component-router>
A component-router
is to define a space where different routes (component-route
) can be defined and are rendered. Each component-router
will only be visibel if a containing route is matched.
The component-loader
is used to dynamicly load a sub component (which is another custom element) all attributes except the path
are passed to the loaded component and can be used there.
Example of a component: component-1.html
<polymer-element name="component-1" attributes="data" noscript>
<template>
<h1>Component - 1</h1>
<span>Show some data: {{data}}</span>
</template>
</polymer-element>
If I've understood correctly, this setup is optimized for pages with a large amount of dynamic content that you only want to load up if a user navigates to a particular route. Using it, you would define one or more
component-route
elements inside your page, each with a number of childcomponent-routes
identifying which routes the component is activated on. I assume that if I have multiplecomponent-route
s on my page with one or more of the same routes that they could work in isolation without this being a problem, right? :)How are you performing the loading mechanism in the
component-loader
?. Are you just usingcore-ajax
or something else? It probably makes sense to usePolymer.import
for this (if you aren't already doing so) per http://addyosmani.github.io/webcomponent-samples/polymer/modules/. This allows you to support inline HTML import definitions being incomponent-1.html
as you would probably hope to use at some point.The only other piece of feedback I can think of is that you seem to have an amount of boilerplate in your element around here:
I wonder if this could be generalized down into a
conditional-loader
element which extendscomponent-loader
. This would allow you to do something like<conditional-loader if="{{route && route.id <= 2}}" path='component-{{route.id}}.html' data="{{route.data}}" ></conditional-loader>
where you could try handling thetemplate
portion behind the scenes. Just a suggestion.