Skip to content

Commit

Permalink
fix: Change router to app scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jun 8, 2023
1 parent 1c3c41b commit 2575f7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
53 changes: 43 additions & 10 deletions packages/masamune/lib/router/router.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
part of masamune;

/// Create an extension method for [RefHasPage] to create nested routers.
/// Create an extension method for [RefHasApp] to create nested routers.
///
/// ネストされたルーターを作成するための[RefHasPage]の拡張メソッドを作成します。
extension MasamuneRouterRefHasPageExtensions on RefHasPage {
/// ネストされたルーターを作成するための[RefHasApp]の拡張メソッドを作成します。
extension MasamuneRouterRefHasPageExtensions on RefHasApp {
/// Create nested routers by passing [pages].
///
/// Pass to [Router.withConfig] to display nested pages.
///
/// Multiple routers can be created by specifying [name].
///
/// [pages]を渡すことによりネストされたルーターを作成します。
///
/// [Router.withConfig]に渡してネストされたページを表示してください。
///
/// [name]を指定することで複数のルーターを作成することが可能です。
///
/// ```dart
/// final router = ref.router(
/// initialQuery: HomePage.query(),
Expand All @@ -26,13 +30,42 @@ extension MasamuneRouterRefHasPageExtensions on RefHasPage {
required RouteQuery? initialQuery,
required List<RouteQueryBuilder> pages,
TransitionQuery? defaultTransitionQuery,
String? name,
}) {
return app.watch(
(ref) {
return NestedAppRouter(
initialQuery: initialQuery,
pages: pages,
defaultTransitionQuery: defaultTransitionQuery,
);
},
name: name,
);
}

/// Get the nested routers created in [router].
///
/// It is possible to manipulate routing from inside the nested pages using the routers obtained.
///
/// If the [router] was created with [name] specified, specify the [name] at the time of creation to obtain the data.
///
/// Returns an error if [NestedAppRouter] does not exist.
///
/// [router]で作成されたネストされたルーターを取得します。
///
/// 取得したルーターを利用してネストされたページ内部からルーティングを操作することが可能です。
///
/// [router][name]を指定して作成した場合は作成時の[name]を指定して取得してください。
///
/// [NestedAppRouter]が存在しない場合はエラーを返します。
NestedAppRouter nestedRouter({
String? name,
}) {
return page.watch((ref) {
return NestedAppRouter(
initialQuery: initialQuery,
pages: pages,
defaultTransitionQuery: defaultTransitionQuery,
);
});
final router = app.fetch<NestedAppRouter>(name);
if (router == null) {
throw Exception("The router does not exist.");
}
return router;
}
}
2 changes: 1 addition & 1 deletion packages/masamune/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ packages:
path: "../katana_model"
relative: true
source: path
version: "2.1.1"
version: "2.2.0"
katana_prefs:
dependency: "direct main"
description:
Expand Down

0 comments on commit 2575f7e

Please sign in to comment.