Skip to content

Commit

Permalink
Feature/destroy scope (#2733)
Browse files Browse the repository at this point in the history
* Add failing test

* Make current scope available in destory callback
  • Loading branch information
SimoTod committed Mar 7, 2022
1 parent c7be8a8 commit 953d85f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/alpinejs/src/directives/x-data.js
Expand Up @@ -34,8 +34,8 @@ directive('data', skipDuringClone((el, { expression }, { cleanup }) => {
reactiveData['init'] && evaluate(el, reactiveData['init'])

cleanup(() => {
undo()

reactiveData['destroy'] && evaluate(el, reactiveData['destroy'])

undo()
})
}))
30 changes: 29 additions & 1 deletion tests/cypress/integration/custom-data.spec.js
Expand Up @@ -159,10 +159,38 @@ test('destroy functions inside custom datas are called automatically',
<div x-data="test">
<button x-on:click="test()"></button>
</div>
<span><span>
<span></span>
`,
({ get }) => {
get('button').click()
get('span').should(haveText('foo'))
}
)

test('destroy have access to the current scope',
html`
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('test', () => ({
destroy() {
document.querySelector('span').textContent = this.foo
},
test() {
Alpine.closestRoot(this.$el).remove()
},
foo: 'bar'
}))
})
</script>
<div x-data="test">
<button x-on:click="test()"></button>
</div>
<span>baz</span>
`,
({ get }) => {
get('span').should(haveText('baz'))
get('button').click()
get('span').should(haveText('bar'))
}
)

0 comments on commit 953d85f

Please sign in to comment.