diff --git a/core/client/composables/store.js b/core/client/composables/store.js index 77bde9ce2..53a3af2e9 100644 --- a/core/client/composables/store.js +++ b/core/client/composables/store.js @@ -21,7 +21,8 @@ export function useStore (name, initialStore) { _.set(store, path, value) } function get (path, defaultValue) { - return _.get(store, path, defaultValue) + // If no path is given return the whole store object + return (path ? _.get(store, path, defaultValue) : store) } function unset (path) { _.unset(store, path) diff --git a/core/client/template-context.js b/core/client/template-context.js index c96bb8f3a..76d1a1e4d 100644 --- a/core/client/template-context.js +++ b/core/client/template-context.js @@ -1,17 +1,17 @@ -// This is a singleton used to inject data in string template evaluation contexts (lodash) -export const TemplateContext = { - initialize () { - if (this.ctx) return - this.ctx = {} - }, +import { useStore } from './composables/store.js' +import { Events } from './events.js' - get () { - return this.ctx - }, +const { store, set, get, unset, has } = useStore('template-context') - merge (ctx) { - this.ctx = Object.assign({}, this.ctx, ctx) +// This is a singleton used to inject data in string template evaluation contexts (lodash) +export const TemplateContext = Object.assign(store, { + get, + has, + unset, + // Override write methods to send events + set (path, value) { + const previousValue = get(path) + set(path, value) + Events.emit('template-context-changed', path, value, previousValue) } -} - -TemplateContext.initialize() +})