Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Allow multiple routers interaction #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@hyperapp/router",
"name": "hyperapp-patched-router",
"description": "Declarative routing for Hyperapp using the History API.",
"version": "0.7.0",
"main": "dist/router.js",
"module": "src/index.js",
"license": "MIT",
"repository": "hyperapp/router",
"repository": "kidandcat/router",
"files": [
"src",
"dist"
Expand Down
7 changes: 7 additions & 0 deletions src/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ function wrapHistory(keys) {
return keys.reduce(function(next, key) {
var fn = history[key]

// Do not wrap if it's already wrapped
if(history[key].toString().indexOf('CustomEvent("pushstate"') !== -1){
return function(){};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess wrapping up a native history object was a bad idea initially...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it works well like it is now, but the thing is, you can't use the router 2 times completely separated, because you will wrap the object two times, then you will have 2 copies of every event.


history[key] = function(data, title, url) {
fn.call(this, data, title, url)
dispatchEvent(new CustomEvent("pushstate", { detail: data }))
Expand Down Expand Up @@ -41,6 +46,8 @@ export var location = {

addEventListener("pushstate", handleLocationChange)
addEventListener("popstate", handleLocationChange)
// Trigger actions.set
handleLocationChange({detail: null});

return function() {
removeEventListener("pushstate", handleLocationChange)
Expand Down