This repository was archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From sveltejs/sapper#262 (comment) Doesn't actually support any props yet though, not sure how I want to set those up just yet.
- Loading branch information
Showing
3 changed files
with
61 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,50 +1,18 @@ | ||
|
||
<a href="#HOME" use:link>home</a> | ||
<a href="#ONE" use:link>one</a> | ||
<a href="#TWO" use:link>two</a> | ||
<a href="#THREE" use:link>three</a> | ||
|
||
<svelte:component this={pages[page]} {children} /> | ||
<svelte:component this={page.child} {...page.props} /> | ||
|
||
<script> | ||
import link from "./link.js"; | ||
|
||
import state from "./state.js"; | ||
|
||
import Home from "./home.html"; | ||
import One from "./one.html"; | ||
import Two from "./two.html"; | ||
import Three from "./three.html"; | ||
|
||
const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1); | ||
|
||
export default { | ||
actions : { | ||
link | ||
}, | ||
|
||
data : () => ({ | ||
page : capitalize(state.state.slice(1)), | ||
children : null, | ||
pages : { | ||
Home, | ||
One, | ||
Two, | ||
Three | ||
} | ||
}), | ||
|
||
oncreate() { | ||
state.on("enter", ({ curr }) => { | ||
const [ , ...parts ] = curr.state.split("/"); | ||
|
||
return this.set({ | ||
page : capitalize(parts[0]), | ||
children : parts.slice(1) | ||
}); | ||
}); | ||
}, | ||
|
||
onstate : console.log.bind(console, "onstate") | ||
}; | ||
</script> |
This file contains 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 |
---|---|---|
@@ -1,5 +1,62 @@ | ||
import state from "./state.js"; | ||
|
||
import App from "./app.html"; | ||
|
||
new App({ | ||
target : document.body, | ||
let app; | ||
|
||
state.on("enter", ({ curr }) => { | ||
// TODO: Assumes single-level component at the root | ||
if(!app) { | ||
app = new App({ | ||
target : document.body, | ||
data : { | ||
page : { | ||
child : curr.component, | ||
props : {}, | ||
}, | ||
}, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Single level component | ||
if(!curr.parent) { | ||
app.set({ | ||
page : { | ||
child : curr.component, | ||
props : {}, | ||
}, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Create the list of nested components | ||
const components = []; | ||
let step = curr; | ||
|
||
while(step.parent) { | ||
components.push(step); | ||
|
||
step = state.get(step.parent); | ||
} | ||
|
||
components.push(step); | ||
|
||
// Create nested data structure to represent the component tree | ||
const data = {}; | ||
|
||
components.reverse().reduce((prev, component) => { | ||
prev.page = { | ||
child : component.component, | ||
props : {}, | ||
}; | ||
|
||
return prev.page.props; | ||
}, data); | ||
|
||
app.set(data); | ||
}); | ||
|
||
state.start(); |
This file contains 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