-
Notifications
You must be signed in to change notification settings - Fork 12
✨ Custom router event target #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsimck
wants to merge
1
commit into
master
Choose a base branch
from
async-listen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@ima/storybook-integration": patch | ||
"@ima/core": minor | ||
--- | ||
|
||
Added support for custom event targets in Router's listen methods. This enables better control over routing behavior by allowing you to: | ||
- Scope navigation handling to specific parts of your application | ||
- Handle multiple independent routed sections on a page | ||
- Better integrate IMA.js routing into existing applications | ||
|
||
Changes: | ||
- Modified `listen(target?: EventTarget)` method to allow for optional target | ||
- Modified `unlisten(target?: EventTarget)` method to allow for optional target | ||
- Added new `unlistenAll()` method to cleanup all event listeners at once |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,13 +37,13 @@ add(name, pathExpression, controller, view, options = undefined); | |
|
||
### name | ||
|
||
> `string` | ||
> `string` | ||
|
||
This argument represents **unique route name**. You can use this name when [linking between routes](./introduction.md#linking-between-routes) or getting the `route` instance using `getRouteHandler()` method. | ||
|
||
### pathExpression | ||
|
||
> `string | object` | ||
> `string | object` | ||
|
||
This can be either `object` for [dynamic routes](./dynamic-routes.md) or `string` representing route path. The pathExpression supports **[parameter substitutions](./introduction.md#route-params-substitutions) | ||
|
||
|
@@ -356,3 +356,44 @@ Custom redirect http status code. | |
> `object = undefined` | ||
|
||
Custom response headers. | ||
|
||
## Custom client router listener root element | ||
|
||
By default, the router listens for navigation events (clicks on links and browser history changes) on the window object. However, you can also specify custom elements to listen on within the initialization function in `main.js`: | ||
|
||
```javascript | ||
const router = oc.get('$Router'); | ||
const appRoot = document.querySelector('#my-app-root'); | ||
|
||
// Listen for navigation events within specific elements | ||
ima | ||
.onLoad() | ||
.then(() => { | ||
ima.reviveClientApp(getInitialAppConfigFunctions(), appRoot); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}) | ||
.catch(error => { | ||
if ($Debug && typeof window !== 'undefined') { | ||
window.__IMA_HMR?.emitter?.emit('error', { error }); | ||
console.error(error); | ||
} | ||
}); | ||
|
||
// ... | ||
// Stop listening on a specific element | ||
router.unlisten(appRoot); | ||
|
||
// Stop listening on all elements | ||
router.unlistenAll(); | ||
``` | ||
|
||
This is particularly useful when you want to: | ||
- Scope navigation handling to specific parts of your application | ||
- Have multiple independent routed sections on a page | ||
- Integrate IMA.js routing into an existing application | ||
- Handle routing in modals or other isolated UI components | ||
|
||
The router will handle clicks on links (`<a>` elements) and popstate events within the specified elements, while ignoring navigation events outside of them. You can add multiple listener roots and manage them independently. | ||
|
||
:::info | ||
When cleaning up your application (for example during unmounting) in a non-standard way, make sure to call `unlistenAll()` to properly remove all event listeners. In the default state the `unlistenAll()` method is called automatically when the instance is destroyed. | ||
::: |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file con
79E1
tains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Příklad je chybný, protože
oc
adocument
nejsou v main.js dostupné.