window.onNuxtReady is now a function

This commit is contained in:
Sébastien Chopin 2016-11-25 15:36:27 +01:00
parent c92ac95371
commit cb3bf4c320
2 changed files with 18 additions and 6 deletions

View File

@ -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)
}
})
}
})
})

View File

@ -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