Skip to content

Instantly share code, notes, and snippets.

View ilikerobots's full-sized avatar

Mike Hoolehan ilikerobots

View GitHub Profile
import 'dart:async';
import 'package:angular2/core.dart';
import 'package:angular2/platform/browser.dart';
import 'package:angular2/router.dart';
@Injectable()
class TitleSetService {
Router _router;
Title _title;
TitleSetService(this._router, this._title) {
_router.subscribe(_setTitleFromRoute);
}
void _setTitleFromRoute(String url) async {
//identify component instruction from routed url
ComponentInstruction compInst = (await _router.recognize(url))?.component;
if (compInst != null) {
_title.setTitle(compInst.routeName);
}
typedef String TitleNamingFunction(ComponentInstruction c);
@Injectable()
class TitleSetService {
TitleNamingFunction nameStrategy;
Router _router;
Title _title;
TitleSetService(this._router, this._title) {
AppComponent(TitleSetService _titleSet) {
_titleSet.nameStrategy = _setTitle;
}
String _setTitle(ComponentInstruction c) {
StringBuffer sb = new StringBuffer();
sb.write("Title Set Demo | ");
if (c.routeData.data.containsKey('title')) { // if title is in data, use it
@RouteConfig(const [
const Route(path: '/players', name: 'Players', component: PlayersComponent, useAsDefault: true),
const Route(path: '/teams', name: 'Teams', component: TeamsComponent),
const Route(path: '/fields', name: 'Fields', component: FieldsComponent,
data: const{'title': 'Ballparks'}),
const Route(path: '/player/:id', name: 'PlayerDetail', component: PlayerDetailComponent,
data: const{'title': 'Player'}),
])
@Injectable()
class SiteStructureService {
static final List<ArticleSection> structure = <ArticleSection>[
new ArticleSection("About Kittens")
..articles.add(new Article("Fuzzy"))
..articles.add(new Article("Warm"))
..articles.add(new Article("Curious")),
new ArticleSection("Anatomy")
..articles.add(new Article("Paws"))
..articles.add(new Article("Whiskers"))
@ilikerobots
ilikerobots / 2016-12-26-developing-angular2-dart-asset-services_routing.dart
Created December 26, 2016 11:01
Building route definitions from SiteStructureService
List<RouteDefinition> _getRouteConfig(List<ArticleSection> pages) {
final List<RouteDefinition> config = <RouteDefinition>[];
for (int i = 0; i < pages.length; i++) {
config.add(
new Route(path: "/" + pages[i].routeSlug,
name: pages[i].routeName,
component: pages[i].component,
data: <String,dynamic>{'id': pages[i].name}));
}
return config;
<div *ngFor="let article of articles" class="content-item">
<h2>{{article.name}}</h2>
<div class="content-html">
<div>Article content goes here</div>
</div>
</div>
import 'dart:async';
abstract class ContentService {
Future<String> getContent(String id);
}
@ilikerobots
ilikerobots / 2016-12-26-developing-angular2-dart-asset-services_placeholder-content-service.dart
Created December 26, 2016 11:06
ContentPlaceHolderService: a simple implementation of ContentService
import 'dart:async';
import 'dart:math';
import 'package:lorem/lorem.dart';
import 'package:angular2/core.dart';
import 'package:angular2_dart_asset_service/src/asset/content/content_service.dart';
@Injectable()
class PlaceholderContentService implements ContentService {
static const int maxDelay = 1500; //max simulated delay in milliseconds
final Random _rnd = new Random();