Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
refactor(routing): simplified routing API
Browse files Browse the repository at this point in the history
Removed ng-bind-route directive, RouteProvider works with ng-view to
find current route and parameters.

Closes #255
  • Loading branch information
pavelgj committed Nov 11, 2013
1 parent 928cdfd commit 6ef8e82
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 216 deletions.
12 changes: 4 additions & 8 deletions lib/routing/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,16 @@
* order to know which recipe to load. Lets consider the following
* `viewRecipe.html`.
*
* <view-recipe ng-bind-route="viewRecipe"></view-recipe>
* <view-recipe></view-recipe>
*
* The template contains a custom `view-recipe` component that handles the
* displaying of the recipe. We also use `ng-bind-route` directive to bind
* that compomnent to "viewRecipe" route. Now, our `view-recipe` can inject
* [RouteProvider] to get hold of the route and its parameters. It might look
* like this:
* displaying of the recipe. Now, our `view-recipe` can inject [RouteProvider]
* to get hold of the route and its parameters. It might look like this:
*
* @NgComponent(...)
* class ViewRecipeComponent {
* ViewRecipeComponent(RouteProvider routeProvider) {
* String recipeId = routeProvider.route.parameters['recipeId'];
* String recipeId = routeProvider.parameters['recipeId'];
* _loadRecipe(recipeId);
* }
* }
Expand Down Expand Up @@ -164,7 +162,6 @@ export 'package:route_hierarchical/client.dart';
part 'routing.dart';
part 'ng_view.dart';
part 'ng_bind_route.dart';
class NgRoutingModule extends Module {
NgRoutingModule({bool usePushState: true}) {
Expand All @@ -180,7 +177,6 @@ class NgRoutingModule extends Module {
// directives
type(NgViewDirective);
type(NgBindRouteDirective);
}
}
Expand Down
105 changes: 0 additions & 105 deletions lib/routing/ng_bind_route.dart

This file was deleted.

102 changes: 85 additions & 17 deletions lib/routing/ng_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ part of angular.routing;
*
* library.html:
*
* <div ng-bind-route="library">
* <div>
* <h1>Library!</h1>
*
* <ng-view></ng-view>
Expand All @@ -56,54 +56,58 @@ part of angular.routing;
* <li><a href="/library/23456/overview">Book 23456</a>
* </ul>
*/
@NgDirective(selector: 'ng-view')
class NgViewDirective implements NgDetachAware {
@NgDirective(
selector: 'ng-view',
publishTypes: const [RouteProvider],
visibility: NgDirective.CHILDREN_VISIBILITY
)
class NgViewDirective implements NgDetachAware, RouteProvider {
final _RoutingHelper locationService;
final BlockCache blockCache;
final Scope scope;
final Injector injector;
final Element element;
RouteHandle route;
bool _showingRoute = false;
RouteHandle _route;

Block _previousBlock;
Scope _previousScope;
Route _viewRoute;

NgViewDirective(Element this.element, RouteProvider routeProvider,
BlockCache this.blockCache, Scope this.scope,
Injector injector, Router router):
injector = injector, locationService = injector.get(_RoutingHelper) {
NgViewDirective(Element this.element, BlockCache this.blockCache,
Scope this.scope, Injector injector, Router router)
: injector = injector, locationService = injector.get(_RoutingHelper) {
RouteProvider routeProvider = injector.parent.get(RouteProvider);
if (routeProvider != null) {
route = routeProvider.route.newHandle();
_route = routeProvider.route.newHandle();
} else {
route = router.root.newHandle();
_route = router.root.newHandle();
}
locationService._registerPortal(this);
_maybeReloadViews();
}

void _maybeReloadViews() {
if (route.isActive) {
locationService._reloadViews(startingFrom: route);
if (_route.isActive) {
locationService._reloadViews(startingFrom: _route);
}
}

detach() {
route.discard();
_route.discard();
locationService._unregisterPortal(this);
}

_show(String templateUrl, Route route) {
assert(route.isActive);

if (_showingRoute) return;
_showingRoute = true;
if (_viewRoute != null) return;
_viewRoute = route;

StreamSubscription _leaveSubscription;
_leaveSubscription = route.onLeave.listen((_) {
_leaveSubscription.cancel();
_leaveSubscription = null;
_showingRoute = false;
_viewRoute = null;
_cleanUp();
});

Expand All @@ -128,4 +132,68 @@ class NgViewDirective implements NgDetachAware {
_previousBlock = null;
_previousScope = null;
}

Route get route => _viewRoute;
String get routeName => _viewRoute.name;
Map<String, String> get parameters {
var res = <String, String>{};
var p = _viewRoute;
while (p != null) {
res.addAll(p.parameters);
p = p.parent;
}
return res;
}
}


/**
* Class that can be injected to retrieve information about the current route.
* For example:
*
* @NgComponent(/* ... */)
* class MyComponent implement NgDetachAware {
* RouteHandle route;
*
* MyComponent(RouteProvider routeProvider) {
* _loadFoo(routeProvider.parameters['fooId']);
* route = routeProvider.route.newHandle();
* route.onRoute.listen((RouteEvent e) {
* // Do something when the route is activated.
* });
* route.onLeave.listen((RouteEvent e) {
* // Do something when the route is diactivated.
* e.allowLeave(allDataSaved());
* });
* }
*
* detach() {
* // The route handle must be discarded.
* route.discard();
* }
*
* Future<bool> allDataSaved() {
* // Check that all data is saved and confirm with the user if needed.
* }
* }
*
* If user component is used outside of ng-view directive then
* injected [RouteProvider] will be null.
*/
abstract class RouteProvider {

/**
* Returns [Route] for current view.
*/
Route get route;

/**
* Returns the name of the current route.
*/
String get routeName;

/**
* Returns parameters for this route.
*/
Map<String, String> get parameters;
}
4 changes: 2 additions & 2 deletions lib/routing/routing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class _RoutingHelper {
if (templateUrl == null) continue;

NgViewDirective view = portals.lastWhere((NgViewDirective v) {
return _routePath(route) != _routePath(v.route) &&
_routePath(route).startsWith(_routePath(v.route));
return _routePath(route) != _routePath(v._route) &&
_routePath(route).startsWith(_routePath(v._route));
}, orElse: () => null);
if (view != null && !alreadyActiveViews.contains(view)) {
view._show(templateUrl, route);
Expand Down
81 changes: 0 additions & 81 deletions test/routing/ng_bind_route_spec.dart

This file was deleted.

Loading

0 comments on commit 6ef8e82

Please sign in to comment.