Skip to content

Commit

Permalink
feat(inertia-sails): remove unneeded methos and have sharedProps as a…
Browse files Browse the repository at this point in the history
… property in sails.inertia
  • Loading branch information
DominusKelvin committed Feb 29, 2024
1 parent e620162 commit 62a3ff0
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions inertia-sails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
const inertia = require('./private/inertia-middleware')
module.exports = function defineInertiaHook(sails) {
let hook
let sharedProps = {}
let sharedViewData = {}
let rootView = 'app'
const routesToBindInertiaTo = [
'GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|',
// (^^Leave out assets)
Expand All @@ -22,37 +19,36 @@ module.exports = function defineInertiaHook(sails) {
return {
defaults: {
inertia: {
rootView: 'app',
version: 1
}
},
initialize: async function () {
hook = this
sails.inertia = hook

sails.inertia.sharedProps = {}
sails.inertia.sharedViewData = {}
sails.on('router:before', function routerBefore() {
routesToBindInertiaTo.forEach(function iterator(routeAddress) {
sails.router.bind(
routeAddress,
inertia(sails, { hook, sharedProps, sharedViewData, rootView })
)
sails.router.bind(routeAddress, inertia(hook))
})
})
},

share: (key, value = null) => (sharedProps[key] = value),
share: (key, value = null) => (sails.inertia.sharedProps[key] = value),

getShared: (key = null) => sharedProps[key] ?? sharedProps,
getShared: (key = null) =>
sails.inertia.sharedProps[key] ?? sails.inertia.sharedProps,

flushShared: (key) => {
key ? delete sharedProps[key] : (sharedProps = {})
key
? delete sails.inertia.sharedProps[key]
: (sails.inertia.sharedProps = {})
},

viewData: (key, value) => (sharedViewData[key] = value),

getViewData: (key) => sharedViewData[key] ?? sharedViewData,

setRootView: (newRootView) => (rootView = newRootView),
viewData: (key, value) => (sails.inertia.sharedViewData[key] = value),

getRootView: () => rootView
getViewData: (key) =>
sails.inertia.sharedViewData[key] ?? sails.inertia.sharedViewData
}
}

0 comments on commit 62a3ff0

Please sign in to comment.