Clear Datastore on Page Unload

Hey all, I've been racking my brain over this for way too long.

I have a multipart form on a separate page (because I already have too many modals :rofl:) and I cannot for the life of me figure out how to clear the datastore I have on page unload.

The reason being is that my data is structured for families (parent table) with 5 child and grandchild tables for students, guardian, emails, phones and addresses. I REALLY don't want write to DB each step, then have to delete. Although I understand this may be the best move if I can't clear my datastore on unload. My foreign keys are set correctly to cascade on delete so that's not a huge issue.

  • I've tried adding dmx.parse('datastore.clear()') on a hunch, but get "Cannot read properties of undefined (reading 'get')"

  • I can't get my windows.addEventListener to fire on unload at all. Yet the script at the bottom of my page to confirm leaving the page works a treat:

window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});

Thanks in advance for any advice.

Cheers
Michael

Hello,

dmx.parse('datastore.clear()')

should work just fine, but probably your expression is wrong. It depends on where is the data store component located and how is it called exactly.
So - what server model are you using?
What page? A full page? A layout with content page?
Where is the data store located? How is it called?
Where did you put the unload function exactly?

Hey Teodor,

The datastore is on a content page.
Using Node v14
The unload function is at the bottom of the content page.

Then your expression is wrong. You are missing the content part:

dmx.parse('content.datastore.clear()')

The data store is located inside the content page, which means you need the content. in front of the expression.
You can easily inspect this writing dmx.app.data in the browser console:

Ah… Perfect. I was wondering where the expressions were relative to. Got it all working now. One of my problems during troubleshooting was the expression being called too early on load and not having the event listener on load.

Thanks Teodor!