From cb3bf4c3201f4a701658b52355836323ec59fb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 25 Nov 2016 15:36:27 +0100 Subject: [PATCH] window.onNuxtReady is now a function --- examples/with-ava/test/index.test.js | 4 ++-- lib/app/client.js | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/with-ava/test/index.test.js b/examples/with-ava/test/index.test.js index 5d3f76364b..0f178ac9e7 100755 --- a/examples/with-ava/test/index.test.js +++ b/examples/with-ava/test/index.test.js @@ -50,9 +50,9 @@ async function renderAndGetWindow (route) { }) } // Used by nuxt.js to say when the components are loaded and the app ready - window.onNuxtReady = function () { + window.onNuxtReady(() => { resolve(window) - } + }) } }) }) diff --git a/lib/app/client.js b/lib/app/client.js index 68505ccb9d..56a34447d3 100644 --- a/lib/app/client.js +++ b/lib/app/client.js @@ -222,6 +222,20 @@ const resolveComponents = flatMapComponents(router.match(path), (Component, _, m return Component }) +// window.onNuxtReady(() => console.log('Ready')) hook +// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading) +let _readyCbs = [] +window.onNuxtReady = function (cb) { + _readyCbs.push(cb) +} +function nuxtReady (app) { + _readyCbs.forEach((cb) => { + if (typeof cb === 'function') { + cb(app) + } + }) +} + Promise.all(resolveComponents) .then((Components) => { const _app = new Vue(app) @@ -233,10 +247,8 @@ Promise.all(resolveComponents) <% } %> // Hot reloading if (module.hot) hotReloadAPI(_app) - // Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading) - if (typeof window.onNuxtReady === 'function') { - window.onNuxtReady(_app) - } + // Call window.onNuxtReady callbacks + nuxtReady(_app) } <% if (store) { %> // Replace store state