1
0
Fork 0

feat(core): Make top-level store methods available through shorthand

This commit is contained in:
Joost De Cock 2022-09-07 19:52:50 +02:00
parent 411f617ffe
commit ef4a70c8f3
2 changed files with 35 additions and 0 deletions

View file

@ -197,6 +197,13 @@ Part.prototype.shorthand = function () {
addCut: this.addCut,
removeCut: this.removeCut,
}
// Add top-level store methods and add a part name parameter
const partName = this.name
for (const [key, method] of Object.entries(this.context.store)) {
if (typeof method === 'function') shorthand[key] = function(...args) {
return method(partName, ...args)
}
}
// We'll need this
let self = this

View file

@ -65,5 +65,33 @@ describe('Store', () => {
expect(pattern.store.get("test.message.warning")).to.equal("hello warning")
expect(pattern.store.get("test.message.info")).to.equal("hello info")
});
it("Should make top-level plugin methods available via shorthand", () => {
const plugin = {
name: 'test',
version: 1,
store: [
['methodA', function(store, name, msg) {
store.set(['test', name, 'a'], msg)
}],
['methodB', function(store, name, msg) {
store.set(['test', name, 'b'], msg)
}],
]
}
const part = {
name: 'example_part',
draft: part => {
const { methodA, methodB } = part.shorthand()
methodA('hello A')
methodB('hello B')
}
}
const Test = new Design({plugins: [plugin], parts: [ part ]})
const pattern = new Test()
pattern.draft()
expect(pattern.store.get("test.example_part.a")).to.equal("hello A")
expect(pattern.store.get("test.example_part.b")).to.equal("hello B")
});
});