Skip to content

Commit

Permalink
feat: Made template context reactive and emit events (closes #999)
Browse files Browse the repository at this point in the history
  • Loading branch information
claustres committed Dec 3, 2024
1 parent b5cd93d commit e5bd488
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion core/client/composables/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions core/client/template-context.js
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit e5bd488

Please sign in to comment.