4
votes

I have started experimenting with Dart and the Route package http://pub.dartlang.org/packages/route.

Two questions have come up immediately:

1) Is it possible (and if what is the idiomatic way) to define a fallback route when the user points the browser to an url that is not supposed to exist (e.g. by giving a false hash fragment). In the example code, the route framework just raises an ArgumentError("No handler found for $path").

2) The dart editor does not serve the compiled application at the root path / but under path derived from a file system directory. This makes it possible to test the route framework. Is it possible to have the application served under, say, localhost:3030/?

2
1) By the looks of it, the server side has a 'defaultStream' but nothing I see on the client side as you mentioned. Perhaps raise a bug? - Matt B

2 Answers

2
votes

for the point 2 :
You can make it like follow ? :
The variable part of the url is set by 'window.location.pathname';

library urls;

import 'dart:html';
import 'package:route/client.dart';

final String _pathName = window.location.pathname;

final UrlPattern _base = new UrlPattern("${_pathName}");
final UrlPattern home = new UrlPattern("${_pathName}#home");
final UrlPattern page2 = new UrlPattern("${_pathName}#page2");

// useFragment: true is important! allow keep '#" un url
// allow to bookmark be valid when browser is closed and reopen.
final Router router = new Router(useFragment: true)
        // simple hack to redirect / to /#home (home UrlPattern)
        ..addHandler(_base, (_) => window.location.hash = "#home");

main() {
  router..addHandler(home, showHome)
        ..addHandler(page2, showPage2)
        ..listen();
}

void showHome(String path) {
  query("body").children
          ..clear()


    ..add(new Element.html("<h1>Home</H1>"));
}

void showPage2(String path) {
  query("body").children
          ..clear()
          ..add(new Element.html("<h1>Page2</H1>"));
}
1
votes

1) I don't know of any good way of doing this. You could listen for every url (.*) and then manually check if the $path matches some other url but that's kinda messy:

 var homeUrl = new UrlPattern(r'/home');
 var contactUrl = new UrlPattern(r'/contact');

 var router = new Router()
   ..addHandler(new url.UrlPattern(r'(.*)'), matchPages)
   ..listen();

 void matchPages(String path) {
   if(homeUrl.matches(path)) {
     // Handle home page display
   } else if(contactUrl.matches(path)) {
     // Contact page
   } else {
     // No match, handle it how you wish
   }
 }

It would be nice to have a built in way of handing default routes on the client.

2) Justin Fagnani (author of the Route package) points out that you can either serve your application from a separate webserver (i.e. not the one provided by the Dart Editor) or use a route that will match the filesystem path as well: (.*)#article will correctly match for article whether the URL is localhost:3030/#article or C:/Dart/app/web//out/#article. There is an open issue for this on Github: https://github.com/dart-lang/route/issues/31