mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-14 20:14:39 +00:00
return a Promise from goto - fixes #270
This commit is contained in:
@@ -133,7 +133,7 @@ function prepare_route(Page: ComponentConstructor, props: RouteData) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function navigate(target: Target, id: number) {
|
function navigate(target: Target, id: number): Promise<any> {
|
||||||
if (id) {
|
if (id) {
|
||||||
// popstate or initial navigation
|
// popstate or initial navigation
|
||||||
cid = id;
|
cid = id;
|
||||||
@@ -299,13 +299,17 @@ export function init(opts: { App: ComponentConstructor, target: Node, routes: Ro
|
|||||||
|
|
||||||
export function goto(href: string, opts = { replaceState: false }) {
|
export function goto(href: string, opts = { replaceState: false }) {
|
||||||
const target = select_route(new URL(href, document.baseURI));
|
const target = select_route(new URL(href, document.baseURI));
|
||||||
|
let promise;
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
navigate(target, null);
|
promise = navigate(target, null);
|
||||||
if (history) history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
|
if (history) history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
|
||||||
} else {
|
} else {
|
||||||
window.location.href = href;
|
window.location.href = href;
|
||||||
|
promise = new Promise(f => {}); // never resolves
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prefetchRoutes(pathnames: string[]) {
|
export function prefetchRoutes(pathnames: string[]) {
|
||||||
|
|||||||
@@ -6,15 +6,21 @@
|
|||||||
|
|
||||||
<p>This is the 'about' page. There's not much here.</p>
|
<p>This is the 'about' page. There's not much here.</p>
|
||||||
|
|
||||||
<button class='goto' on:click='goto("blog/what-is-sapper")'>What is Sapper?</button>
|
|
||||||
<button class='prefetch' on:click='prefetch("blog/why-the-name")'>Why the name?</button>
|
<button class='prefetch' on:click='prefetch("blog/why-the-name")'>Why the name?</button>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { goto, prefetch } from '../../../runtime.js';
|
import { goto, prefetch } from '../../../runtime.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
oncreate() {
|
||||||
|
window.goto = goto;
|
||||||
|
},
|
||||||
|
|
||||||
|
ondestroy() {
|
||||||
|
window.goto = null;
|
||||||
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
goto,
|
|
||||||
prefetch
|
prefetch
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -281,9 +281,7 @@ function run({ mode, basepath = '' }) {
|
|||||||
return nightmare
|
return nightmare
|
||||||
.goto(`${base}/about`)
|
.goto(`${base}/about`)
|
||||||
.init()
|
.init()
|
||||||
.click('.goto')
|
.evaluate(() => window.goto('blog/what-is-sapper'))
|
||||||
.wait(url => window.location.pathname === url, `${basepath}/blog/what-is-sapper`)
|
|
||||||
.wait(100)
|
|
||||||
.title()
|
.title()
|
||||||
.then(title => {
|
.then(title => {
|
||||||
assert.equal(title, 'What is Sapper?');
|
assert.equal(title, 'What is Sapper?');
|
||||||
|
|||||||
Reference in New Issue
Block a user