Last active
January 3, 2016 14:39
-
-
Save a14n/8477285 to your computer and use it in GitHub Desktop.
<x-route> polymer component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<link rel="import" href="route.html"> | |
<script type="application/dart">export 'package:polymer/init.dart';</script> | |
<a href="#/home">Home</a> | |
<a href="#/list">List</a> | |
<hr> | |
<x-route></x-route> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:polymer/polymer.dart'; | |
import 'package:route/client.dart'; | |
@CustomTag('x-route') | |
class XRoute extends PolymerElement { | |
// Whether styles from the document apply to the contents of the component | |
bool get applyAuthorStyles => true; | |
XRoute.created() : super.created() { | |
var router = new Router() | |
..addHandler(homeUrl, _routeHandler(homeUrl)) | |
..addHandler(listUrl, _routeHandler(listUrl)) | |
..listen(); | |
route = new Route(homeUrl); | |
} | |
final homeUrl = new UrlPattern(r'/(.*)#/home'); | |
final listUrl = new UrlPattern(r'/(.*)#/list'); | |
@observable Route route; | |
Handler _routeHandler(UrlPattern url) => (String path) { | |
print("Route changed: $url - $path"); | |
route = new Route(url, url.parse(path)); | |
}; | |
int asInt(String value) => int.parse(value); | |
} | |
class Route { | |
final UrlPattern url; | |
final List params; | |
Route(this.url, [this.params = const []]); | |
operator [](int index) => index < params.length ? params[index] : null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<polymer-element name="x-route"> | |
<template> | |
<template if="{{route.url == homeUrl}}"> | |
Home | |
</template> | |
<template if="{{route.url == listUrl}}"> | |
List | |
</template> | |
</template> | |
<script type="application/dart" src="route.dart"></script> | |
</polymer-element> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment